【问题标题】:Error fetching OAuth2 access token - invalid_grant. BUT it is VALID获取 OAuth2 访问令牌时出错 - invalid_grant。但它是有效的
【发布时间】:2016-03-02 03:45:30
【问题描述】:

尝试在带有后端服务器的 iOS 上使用 Google 进行身份验证,代码用于与旧版 Google 客户端库一起使用。

$client = new Google_Client();
$oauth2 = new Google_Service_Oauth2($client);
$client->setApplicationName('MyApp');
$client->setClientId($google_config["clientId"]);
$client->setClientSecret($google_config["clientSecret"]);
$client->setRedirectUri("");
$client->setDeveloperKey($google_config["developerKey"]));
Log::error("Google/authcode", $request->get('token'));
if (! isset($_SESSION['access_token'])) {
    try {
    $client->authenticate($request->get('token'));
    } .... THROWS exception: Error fetching OAuth2 access token, message: 'invalid_grant:

尝试验证令牌:

https://www.googleapis.com/oauth2/v1/tokeninfo?id_token= https://www.googleapis.com/oauth2/v2/tokeninfo?id_token= https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=

所有工作都成功表明令牌是有效的。

编辑: 添加 verifyIdToken 调用后才发现: 发行人无效,https://accounts.google.com != accounts.google.com 似乎这就是我失败的原因。我猜 iOS sdk 与 PHP 的设置不同?或者这可能不相关,并假设它不是来自 iOS 设备的服务器授权。因为如果我不断更改服务器端代码的内容,即将 OAUTH2_ISSUER 更改为 https://accounts.google.com,我会收到错误的收件人错误,这是有道理的,因为我使用的是 iOS 客户端获取的 tokenid 来授权服务器。

旧版本的 iOS 应用也可以正常工作。但是令牌完全不同,尽管验证也很好。 非常感谢任何帮助。

【问题讨论】:

    标签: ios oauth-2.0 token google-client


    【解决方案1】:

    我想我误解了文档。为了授权服务器,需要从

    传递 googleUser.serverAuthCode
    - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)googleUser
     withError:(NSError *)error { ....
    

    如果仅使用 iOS 端用户 googleUser.authentication.accessToken 对服务器进行授权验证后端服务器,则需要在服务器上进行验证或解析以获取电子邮件和其他数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-26
      • 2016-03-05
      • 1970-01-01
      • 2012-07-14
      • 2014-09-04
      • 2021-09-11
      • 2014-06-28
      • 2015-12-08
      相关资源
      最近更新 更多