使用有效的access_token时缺少令牌端点URI

时间:2016-06-29 03:15:26

标签: ruby-on-rails oauth-2.0 google-api

突然(可能是宝石更新?)调用Google api停止了工作。 我已经能够检索gmail线程,但不再是。

到目前为止我所拥有的:

我可以检索刷新令牌以进行离线访问。所以我存储了刷新和访问令牌。大。

现在我想要检索个人资料信息。这就是我做的事(请注意,这曾经很好用!)

我将在下面的每一行之后添加一个看跌期权,以向您展示我们得到的......

module AuthHelper
  require 'google/api_client/client_secrets'
  ...
  client = Signet::OAuth2::Client.new(access_token: user.gmail_access_token)
  service = Google::Apis::GmailV1::GmailService.new
  service.authorization = client
  service.get_user_profile('me') # retrieve threads
  ...
end

让我打印出我得到的回复:

CLIENT : --- !ruby/object:Signet::OAuth2::Client
authorization_uri: 
token_credential_uri: 
client_id: 
client_secret: 
code: 
expires_at: 
expires_in: 
issued_at: 
issuer: 
password: 
principal: 
redirect_uri: 
scope: 
state: 
username: 
expiry: 60
extension_parameters: {}
additional_parameters: {}
access_token: ya29.Ci8QA3K_mLh1vB_55bSnGVB66yLwzkxClM8yCI5qmmZo6n0JzjVNA7Q6EAOm3qQeGw

SERVICE : --- !ruby/object:Google::Apis::GmailV1::GmailService
root_url: https://www.googleapis.com/
base_path: gmail/v1/users/
upload_path: upload/gmail/v1/users/
batch_path: batch
client_options: !ruby/struct:Google::Apis::ClientOptions
  application_name: unknown
  application_version: 0.0.0
  proxy_url: 
  use_net_http: false
request_options: !ruby/struct:Google::Apis::RequestOptions
  authorization: 
  retries: 0
  header: 
  timeout_sec: 
  open_timeout_sec: 20

SERVICE.authorization : --- !ruby/object:Signet::OAuth2::Client
authorization_uri: 
token_credential_uri: 
client_id: 
client_secret: 
code: 
expires_at: 
expires_in: 
issued_at: 
issuer: 
password: 
principal: 
redirect_uri: 
scope: 
state: 
username: 
expiry: 60
extension_parameters: {}
additional_parameters: {}
access_token: ya29.Ci8QA3K_mLh1vB_55bSnGVB66yLwzkxClM8yCI5qmmZo6n0JzjVNA7Q6EAOm3qQeGw
{"message":"Sending HTTP get https://www.googleapis.com/gmail/v1/users/me/profile?","@timestamp":"2016-06-28T19:52:38.974-07:00","@version":"1","severity":"DEBUG","host":"ben.local"}
{"message":"Caught error Missing token endpoint URI.","@timestamp":"2016-06-28T19:52:38.976-07:00","@version":"1","severity":"DEBUG","host":"ben.local"}
{"message":"Error - #\u003cArgumentError: Missing token endpoint URI.\u003e\n","@timestamp":"2016-06-28T19:52:38.977-07:00","@version":"1","severity":"DEBUG","host":"ben.local"}
GOT EXCPETION: Missing token endpoint URI.

我不应该看到:“缺少令牌端点URI”。当你使用access_token时,它应该处理授权,对吧?正如我所说,这不是一个问题。

虽然我认为不需要,但我在clients_secret.json中添加了端点URI“https://accounts.google.com/o/oauth2/auth”并且授权失败。

我错过了什么? :)

3 个答案:

答案 0 :(得分:9)

Google文档删除了应该解释如何在他们的所有在线教程中获得访问令牌后如何访问其api的部分!在这里:

  1. https://github.com/google/google-api-ruby-client
    • 停止解释如何通过此评论进行身份验证:“drive.authorization = ...#查看Googleauth或Signet库”所以我们正在前往Signet,因为Googleauth很神秘。
  2. https://github.com/google/signet
    • 指导您如何获取访问令牌,然后停止解释如何处理它。
  3. https://developers.google.com/identity/protocols/OAuth2WebServer#offline
    • Google的API文档,向您展示如何获取访问令牌,甚至是刷新的离线访问(我有),然后是NO 关于如何处理访问令牌的解释。
  4. 我终于在这里找到了答案:https://github.com/google/google-api-ruby-client/issues/296

    基本上为访问令牌创建一个新类(我将它添加到我的lib文件夹中)并按照上面链接中描述的其余内容进行操作。只用.authorize替换.authenticate,我想这是一个错误。希望这有助于某人。

答案 1 :(得分:0)

以下线节省矿时间。现在工作正常

添加:

client.expires_in = Time.now + 1_000_000

后:

client = Signet::OAuth2::Client.new(access_token: user.gmail_access_token)

答案 2 :(得分:0)

从github问题页面复制代码以节省您的点击次数:

require 'google/apis/sheets_v4'
require 'google/apis/drive_v2'
require 'googleauth'

class GoogleService
  attr_accessor :sheets, :drive

  def initialize(user)
    auth = Signet::OAuth2::Client.new(
  token_credential_uri: 'https://oauth2.googleapis.com/token',
  client_id: ENV["GOOGLE_CLIENT_ID"],
  client_secret: ENV["GOOGLE_CLIENT_SECRET"],
  refresh_token: user.refresh_token
)
auth.fetch_access_token!
user.update(token: auth.access_token)


sheets = Google::Apis::SheetsV4::SheetsService.new
sheets.authorization = auth
@sheets = sheets

drive = Google::Apis::DriveV2::DriveService.new
drive.authorization = auth
@drive = drive

结束 结束