【问题标题】:Google+ php api errorGoogle+ php api 错误
【发布时间】:2014-12-19 20:00:44
【问题描述】:

**已解决**

我怎样才能让 Google+ PHP API 与代码点火器一起工作,只是调用了 autoload.php 或客户端不起作用。我认为当 google 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->authenticate('4/zfxGdy7q1VhTj...') 1 /home/my_name/public_html/application/models/login_model.php(37): Google_Client->authenticate('4/zfxGdy7q1VhTj...') 2 /home/my_name/public_html/application/controllers/Store.php(3916): Login_model->google_validate() 3【内部函数】:Store->google_login() 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_once('/home/nemesisfo...') 6 {main} 抛出 /home/my_name/public_html/google-api-php-client-master/src/Google/Auth/OAuth2.phpon 第 120 行

有什么想法吗?

【问题讨论】:

  • 您不应与 '$this' 发生冲突,因为它应保持在其范围内。问题--> 你把这段代码放在哪里?控制器?图书馆?
  • 它在我的登录模型中,它使用 ajax 返回一个值,然后 javascript 输出结果
  • 请将您的解决方案作为答案发布并接受它

标签: php google-api-php-client


【解决方案1】:

嗯,我终于想通了,显然没有什么地方可以告诉你我必须做什么,或者我只是错过了。

添加这个非常重要:$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);

【讨论】:

    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 2016-11-22
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2022-01-07
    • 2017-07-28
    相关资源
    最近更新 更多