【发布时间】:2015-06-03 21:14:08
【问题描述】:
我有一个使用 oauth gem 从 google oauth1 迁移到 oauth2 的脚本
一切似乎都正确,我无法检测到标题有任何问题,但它仍然响应:Invalid authorization header.
这是标题:
正在打开与 accounts.google.com:443 的连接... 打开 正在为 accounts.google.com:443 启动 SSL... 建立 SSL /\r\n用户代理:OAuth gem v0.4.7\r\n内容长度:193\r\n授权:OAuth oauth_consumer_key=\"mykey.com\ ", oauth_nonce=\"LdBeaxxxxxxxxxxxxxxIxgd03U1DHYbs\", oauth_signature=\"wXIuxxxxxxxyxxxxj0%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1416497830\", oauth_token=\"1%2F9G2sKmQxxxxxxxxxxmDXbqqifoRBGUAii-D5sw2o\", oauth_version=\"1.0\"\r\n连接:关闭\r\n主机:accounts.google.com\r\n\r\n"
"HTTP/1.1 400 错误请求\r\n"
-> "Content-Type: application/json\r\n"
-> "Cache-Control: no-cache, no-store, max-age=0, must-revalidate\r\n"
-> "Pragma: no-cache\r\n"
->“过期时间:1990 年 1 月 1 日星期五 00:00:00 GMT\r\n”
->“日期:2014 年 11 月 20 日星期四 12:37:03 GMT\r\n”
-> "X-Content-Type-Options: nosniff\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
->“服务器:GSE\r\n”
-> "备用协议:443:quic,p=0.01\r\n"
-> "连接:关闭\r\n"
-> "\r\n"
阅读全文...
-> "{\n \"error\" : \"invalid_request\",\n \"error_description\" : \"无效授权 标题。\"\n}"
读取 90 个字节 康恩关闭
这是代码:
oauth1_consumer_key = "mykey"
oauth1_consumer_secret = "Gxxxxxxxxxxxxxxxxqb8"
# OAuth 1 - User Token / Secret.
oauth1_token = '1/9G2xxxxxxxxxxxxxxsw2o'
oauth1_secret = 'a-xxxxxxxxxxxxxxxmqG'
# OAuth 2 - Application ID / Secret
oauth2_client_id = "the_id"
oauth2_client_secret = "C-xxxxxxxxxxxxxxxxV"
# Migration Parameters.
params = {
"grant_type" => "urn:ietf:params:oauth:grant-type:migration:oauth1",
"client_id" => oauth2_client_id,
"client_secret" => oauth2_client_secret,
# "oauth_signature_method" => "HMAC-SHA1"
}
# Create the consumer object.
consumer = OAuth::Consumer.new(
oauth1_consumer_key,
oauth1_consumer_secret,
:site => 'https://accounts.google.com',
:scheme => :header
)
# Create the access token object.
access_token = OAuth::AccessToken.new(consumer, oauth1_token, oauth1_secret)
resp = access_token.post(
"/o/oauth2/token",
params,
{ 'Content-Type' => 'application/x-www-form-urlencoded' })
if resp.code.to_s != "200"
# Raise an error.
raise "#{resp.code} - #{resp.body}"
end
有什么想法吗?
【问题讨论】:
-
嗨,我已经验证了你的代码。有用。不知道你是否还有问题
-
只有一个区别,我的帖子网址是 googleapis.com/oauth2/v3/token
-
是的,它现在正在工作!很奇怪。还是谢谢你:)
标签: ruby-on-rails ruby oauth google-oauth