Oauth2令牌不适用于gmail_xoauth gem [英] Oauth2 token not working for gmail_xoauth gem

查看:62
本文介绍了Oauth2令牌不适用于gmail_xoauth gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过omniauth-google-oauth2宝石通过以下方式获得令牌: https://github.com/zquestz/omniauth-google-oauth2

I'm currently getting a token via the omniauth-google-oauth2 gem per the following: https://github.com/zquestz/omniauth-google-oauth2

我存储了从auth_hash返回的令牌.

I store the token that comes back from the auth_hash.

然后我尝试通过以下方式使用令牌:

I then try to use the token by calling:

require 'gmail_xoauth'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', 'myemail@gmail.com', Token.last)

问题是我得到一个错误:

Problem is that I get an error:

[8] pry(main)> imap.authenticate('XOAUTH2', 'myemail@gmail.com', Token.last)
  Token Load (0.6ms)  SELECT  "tokens".* FROM "tokens"  ORDER BY "tokens"."id" DESC LIMIT 1
Net::IMAP::NoResponseError:  Invalid credentials (Failure)
from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/imap.rb:1171:in `get_tagged_response'

推荐答案

好吧,我看了google-api-client,然后开始多使用它.目前看来最好的解决方案是执行以下操作:

Ok I took a look at google-api-client and I started to use that a little more. Seems like the best solution at the moment is to do the following:

  1. 创建一个包含gmail parameters和gmail api_method
  2. 的选项哈希
  3. 使用通过access_token初始化的服务在options哈希中创建查询
  1. create an options hash that includes the gmail parameters and the gmail api_method
  2. use the service you initialize with your access_token to create your query in the options hash

下面您将看到一个可以使用的粗略示例

Below you can see a rough example that you can use

client = Google::APIClient.new
client.authorization.access_token = token
service = client.discovered_api 'gmail'
options = {parameters: {'userId' => 'email@gmail.com'}, api_method: service.users.messages.list}
client.execute(options)

我的意思是,这很笨拙,应该将其干燥. (希望谷歌围绕此创建更好的gem)

I mean this is pretty clunky and should be DRY'd up. (Hopefully google creates a better gem around this)

这篇关于Oauth2令牌不适用于gmail_xoauth gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆