突然(可能是gem更新?)调用Google api停止工作。我已经能够检索gmail的线程,但再也不能了。
目前为止我有什么:
我能够检索离线访问的刷新令牌。所以我存储了刷新和访问令牌。很好。
现在我想检索概要信息。我是这样做的(提醒你,这曾经很好!)
我将在下面的每一行后面添加一个put,以显示我们得到了什么…
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.u003en","@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添加了端点URI 'https://accounts.google.com/o/oauth2/auth'。Json和get授权失败。
我错过了什么?:)
Google文档删去了应该解释如何访问他们的api的部分,一旦你真正从他们所有的在线教程中获得访问令牌!:
- https://github.com/google/google-api-ruby-client
- 停止用注释解释如何进行身份验证:"drive。授权=…#查看Googleauth或Signet库"所以我们转向Signet,因为Googleauth是神秘的。
- https://github.com/google/signet
- 指导您如何获得访问令牌,然后停止解释如何使用它。
- https://developers.google.com/identity/protocols/OAuth2WebServer离线
- Google的API文档,向您展示了如何获得访问令牌,甚至是具有刷新的离线访问(我有),然后NO说明如何使用访问令牌。
我终于在这里找到了答案:https://github.com/google/google-api-ruby-client/issues/296
基本上为访问令牌创建一个新类(我将其添加到我的lib文件夹中),并按照上面链接中描述的其余内容进行操作。只把。authenticate替换成。authorize,我想这是一个错误。
下面一行节省我的时间。现在运行正常
添加:
client.expires_in = Time.now + 1_000_000
:后
client = Signet::OAuth2::Client.new(access_token: user.gmail_access_token)
从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
结束结束