Google+ php api error


Google+ php api error

**已解决**

我如何让Google+PHP API与代码点火器一起工作,只调用autoload.PHP或客户端不工作。我认为,当谷歌api使用"$this"来引用其类,但它最终调用了一个不存在的代码点火器类时,在某些地方会发生冲突。

我尝试使用库进行集成,但只能让客户端工作,我也需要Google_HttpRequest来工作。

这是我正常尝试过的,但都不起作用

require_once getcwd().'/google-api-php-client-master/autoload.php';
    $client = new Google_Client();
    $client->setApplicationName('app_name');
    $client->setClientId('my_client_id');
    $client->setClientSecret('my_client_secret');
    $client->setDeveloperKey('my_developer_key');
    $client->authenticate($auth_code);
    $access_token = $client->getAccessToken();
    echo $access_token;

这就是我得到的错误:

致命错误:未捕获异常"Google_Auth_exception",消息为'获取OAuth2访问令牌时出错,消息:中的'invalid_request''/home/my_name/public_html/google-api-php-client-master/src/google/Auth/OAuth2.php:120

堆栈跟踪:

0/home/my_name/public_html/google-api-php-client-master/src/google/client.php(120):Google_Auth_OAuth2->身份验证('4/zfxGdy7q1VhTj…')1/home/my_name/public_html/application/models/login_model.php(37):Google_Client->身份验证('4/zfxGdy7q1VhTj…')2/home/my_name/public_html/application/controllers/Store.php(3916):登录模型->谷歌验证()3[内部功能]:存储->谷歌登录()4/home/my_name/public_html/system/core/CodeIgniter.php(359):call_user_func_array(array,array)5/home/my_name/public_html/index.php(223):require_one('/home/nemesisfo…')在/home/my_name/public_html/google-api-php-client-master/src/google/Auth/OAuth2.phpon中抛出6{main}120线

有什么想法吗?

我终于明白了,显然没有什么地方告诉你我必须做什么,或者我错过了

添加以下内容非常重要:$client->setRedirectUri('postmessage');

此外,不需要将其作为库实现到代码点火器中,您可以像往常一样设置它。

所以最终这就是所需要的:

//We need to load the Google api library
    require_once getcwd().'/google-api-php-client-master/autoload.php';
    $client = new Google_Client();
    $client->setApplicationName('app name');
    $client->setClientId('your client id');
    $client->setClientSecret('your client secret');
    $client->setDeveloperKey('its actaully just the API key under key for server apps');
    $client->setRedirectUri('postmessage'); //DONT FORGET IT, its the same redirecturi set on the javascript side for one time code flow apps
    //Now to get the access token by exchanging the one time code
    $client->authenticate($auth_code);
    $token = json_decode($client->getAccessToken());
    // Verify the token
    $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$token->access_token;
    $req = new Google_Http_Request($reqUrl);
    $tokenInfo = json_decode($client->getAuth()->authenticatedRequest($req)->getResponseBody());

    print_r($tokenInfo);

还有另一件延迟的事情是,对于一次性代码流,文档中有这样的

$tokenInfo = json_decode($client::getIo()->authenticatedRequest($req)->getResponseBody());

事实上,它需要这样,至少对我来说,让它工作

$tokenInfo = json_decode($client->getAuth()->authenticatedRequest($req)->getResponseBody());

此外,我不得不将Request.php中的类Google_HttpRequest重命名为类Google_Http_Request,因为autoload.php的工作方式,它将名称拆分为"_"。

那就这样称呼它吧:

$req = new Google_Http_Request($reqUrl);