【问题标题】:Net::Google::DataAPI::Auth::OAuth2 failed oauth call access token: received error: 400 Bad RequestNet::Google::DataAPI::Auth::OAuth2 失败 oauth 调用访问令牌:收到错误:400 错误请求
【发布时间】:2015-06-01 02:26:32
【问题描述】:

我在这里关注解决方案: Net::Google::AuthSub login failed with new Google Drive version

但是,在这一行:

my $token = $oauth2->get_access_token($code) or die;

我收到以下错误:

failed oauth call access token: received error: 400 Bad Request
{
  "error" : "invalid_request"
}

可能是什么问题?我需要修复 Net::Google::DataAPI::Auth::OAuth2 中的内容吗? 我在 Windows 中使用 Strawberry Perl。

编辑: 以下是我的代码:

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
  client_id => 'XXXXXXX.apps.googleusercontent.com',
  client_secret => 'XXXXXXXX',
  scope => ['http://spreadsheets.google.com/feeds/'],
);

my $url = $oauth2->authorize_url();
print "OAuth URL, get code: $url\n";
use Term::Prompt;
my $code = prompt('x', 'paste the code: ', '', ''); 

my $token = $oauth2->get_access_token($code) or die;

【问题讨论】:

  • 您是否将从网络浏览器获取的代码准确地粘贴到您的应用程序中?
  • 是的,代码以 4/ 开头,以 mwI 结尾。共 77 个字符。
  • 也许 Windows 在将代码复制到 code 变量之前对其进行了解释。尝试将代码直接粘贴到您的应用程序中,将 $code = prompt(...) 行替换为 $code = "... paste from browser..."

标签: perl google-spreadsheet-api google-authentication google-oauth


【解决方案1】:

当我这样做时,我总是得到以 4/ 开头并以 mwI# 结尾的代码。也许你错过了最后一个 # 字符?

【讨论】:

  • 我在代码末尾添加了#,但仍然有同样的错误。由于我进入了谷歌页面,上面写着“请复制此代码,切换到您的应用程序并将其粘贴到那里:”,这意味着 client_id 和 client_secret 应该已正确设置,对吧?除了我提到的解决方案页面中建议的设置之外,是否还有需要启用的帐户设置?
  • 嗯,不确定。您可以发布更多代码吗? (当然,去掉 client_id 和 client_secret。)您也可以在 OAuth Playground 上developers.google.com/oauthplayground 尝试您的代码,看看您是否在那里获得了令牌。
  • 我把我的代码放在上面。我去了 OAuth Playground,响应:HTTP/1.1 401 Unauthorized Content-length: 74 X-xss-protection: 1; mode=block X-content-type-options: nosniff Expires: Tue, 02 Jun 2015 03:05:55 GMT Vary: Origin,X-Origin Server: GSE Cache-control: private, max-age=0 Date: Tue, 2015 年 6 月 2 日 03:05:55 GMT X-frame-options: SAMEORIGIN Content-type: application/json; charset=UTF-8 Www-authenticate: Bearer realm="accounts.google.com" { "error_description": "Unauthorized", "error": "unauthorized_client" }
  • 上面的草稿评论,我正在使用我的脚本中的代码并尝试授权 OAuth Playground,当然它会说未经授权的客户端。
  • 查看您的代码,与我的唯一区别是它要求我在对Net::Google::DataAPI::Auth::OAuth2->new 的初始调用中将redirect_uri 设置为参数。但是,如果您要找回密码,那您肯定没问题。我想知道 Edward Doolittle 的上述评论是否在正确的轨道上,这与 Windows 中的 Term::Prompt 行为有关,这使事情变得混乱。尝试让您的脚本在读取代码后将其打印出来,和/或将您的脚本一分为二(一个用于获取代码,另一个用于将代码粘贴到硬编码的脚本中)。
猜你喜欢
  • 1970-01-01
  • 2013-07-21
  • 2016-12-03
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
相关资源
最近更新 更多