首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“错误”:"invalid_grant","error_description":“坏请求”当我试图使用gmail登录时

“错误”:"invalid_grant","error_description":“坏请求”当我试图使用gmail登录时
EN

Stack Overflow用户
提问于 2022-06-29 07:26:01
回答 1查看 189关注 0票数 0

这是我的get访问令牌方法,\XLite\Core\HTTP\Request(static::TOKEN_REQUEST_URL);保护函数getAccessToken($code) { $request =新的$request

代码语言:javascript
运行
复制
$request->body = array(
    'code'          => $code,
    'client_id'     => $this->getClientId(),
    'client_secret' => $this->getClientSecret(),
    'redirect_uri'  => $this->getRedirectUrl(),
    'grant_type'    => 'authorization_code',
);


$response = $request->sendRequest();

$accessToken = null;
if (200 == $response->code) {
    $data = json_decode($response->body, true);
    $accessToken = $data['access_token'];
}

return $accessToken;

}

,这里我在日志中得到了响应:

代码语言:javascript
运行
复制
[06:35:13.000000] array (
  'request response from google' => 
  (object) array(
     '__CLASS__' => 'PEAR2\\HTTP\\Request\\Response',
     'code' => 400,
     'headers' => 
    (object) array(
       '__CLASS__' => 'PEAR2\\HTTP\\Request\\Headers',
       'iterationStyle' => 'lowerCase',
       'fields:protected' => 'Array(13)',
       'camelCase:protected' => NULL,
       'lowerCase:protected' => NULL,
    ),
     'cookies' => 
    array (
    ),
     'body' => '{
  "error": "invalid_grant",
  "error_description": "Bad Request"
}',
     'scheme' => 'https',
     'host' => 'accounts.google.com',
     'path' => '/o/oauth2/token',
     'uri' => 'https://accounts.google.com/o/oauth2/token',
     'port' => 443,
  ),
)

如果您需要任何其他东西,请告诉我,您可以看到响应体。

EN

回答 1

Stack Overflow用户

发布于 2022-06-29 07:40:42

post正文作为查询字符串发送,而不是数组。

代码语言:javascript
运行
复制
POST https://accounts.google.com/o/oauth2/token
code=AuthorizationCode&client_id=ClientId}&client_secret={ClientSecret}&redirect_uri=RedirectURI&grant_type=authorization_code

记住,代码将在五分钟内过期,只能使用一次。

我在php中表示,您应该在post数据中使用http-构建-查询

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72797366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档