致命错误:未捕获的异常“Google_Auth_Exception”,并显示错误“获取OAuth2访问令牌时出错,消息:'invalid_client''

时间:2014-12-11 10:13:09

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

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

当我尝试验证令牌时,我收到了致命错误说:

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();  
    ?>

1 个答案:

答案 0 :(得分:-1)

对于$google_client_secret,而不是'notasecret'使用"CLIENT SECRET"的{​​{1}}部分中的值。