【问题标题】:Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client''致命错误:未捕获的异常“Google_Auth_Exception”和消息“获取 OAuth2 访问令牌时出错,消息:'invalid_client'”
【发布时间】:2014-12-11 10:13:08
【问题描述】:

我正在开发一个使用 googleplus 登录的移动应用程序。我在其中获取访问令牌并将其发送到我的服务器,以便使用 Google APIs Client Library for PHP 对其进行验证。

当我尝试验证令牌时,我收到 Fatal Error Saying :

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php:127 Stack trace: #0 C:\xxx\vendor\google\apiclient\src\Google\Client.php(130): Google_Auth_OAuth2->authenticate('ya29.2QBihTdAag...') #1 C:\xxx\index.php(20): Google_Client->authenticate('ya29.2QBihTdAag...') #2 {main} thrown in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php on line 127

这是我的 PHP 代码:

<?php
    $google_client_id       = 'XXXXX.apps.googleusercontent.com';
    $google_client_secret   = 'XXXXX';
    $google_redirect_url    = 'xxx';
    $google_developer_key   = 'XXXXXXXX';
    $google_application_name = 'XXXX Login';
    $google_application_scope = 'email'; /* I only needed the basic user info */
    $google_redirect_uri='';
    //include google api files
    require_once 'vendor/autoload.php';

    $gClient = new Google_Client();
    $gClient->setApplicationName($google_application_name);
    $gClient->setClientId($google_client_id);
    $gClient->setClientSecret($google_client_secret);
    $gClient->setRedirectUri($google_redirect_uri);
    $gClient->setScopes($google_application_scope);
    $gClient->setDeveloperKey($google_developer_key);

    $gClient->authenticate("ya29.2QBihTdAagwpH5hHJUg5soIETLspVUgle6VPOT52UYPWkOKyHdUJlpUyvoMKPgkuqIS4PvgXQhfpHw");//Error Happened here
    $token = json_decode($gClient->getAccessToken());

    $google_oauthV2 = new Google_Service_Oauth2($gClient);
   $user_info = $google_oauthV2->userinfo->get();
   var_dump(user_info);
   die();  
    ?>

【问题讨论】:

  • 您的客户端ID和密钥是否正确?
  • @Anju,我使用的是“Web 应用程序的客户端 ID”而不是“Android 应用程序的客户端 ID”的客户端 ID 和密码。
  • 您是否按照这些步骤操作smarttutorials.net/… 。仔细检查客户端和密钥,开始和结束没有空格
  • @Anju 谢谢,但我是从安卓应用程序发送令牌,而不是从浏览器
  • 我遇到了同样的错误。它与令牌有关。 Android 调用“GoogleAuthUtil.getToken”仅返回访问令牌字符串的一部分或其他内容。直接 getInfo 调用有效。我正在调查。

标签: php google-api google-api-php-client


【解决方案1】:

对于$google_client_secret,而不是'notasecret',请使用"Client ID for web application""CLIENT SECRET" 部分中的值。

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多