简体   繁体   中英

Google+ php api error

**SOLVED **

How can I get the Google+ PHP API to work with code igniter, just called the autoload.php or client does not work. I think there is a conflict somewhere when the google api uses '$this' to refer to its class but it ends calling a code igniter class that does not exist.

I tried integrating using libraries but could only get client to work, I need Google_HttpRequest to work as well.

here is what I have tried normally and it doesn't even work

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;

and this is the error I get:

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_request'' in /home/my_name/public_html/google-api-php-client-master/src/Google/Auth/OAuth2.php:120

Stack trace:

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 [internal function]: 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} thrown in /home/my_name/public_html/google-api-php-client-master/src/Google/Auth/OAuth2.phpon line 120

So any ideas?

Well I finally figured it out, apparently there is like nowhere that tells you what I had to do or I just missed it.

It is very important that you add this: $client->setRedirectUri('postmessage');

Also there is no need to implement it into code igniter as a library you can just set it up like usual.

So in the end this is all that was needed:

//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);

Also another retarded thing is in there documentation for for one time code flow, they do this

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

well it actually needs to be this, atleast for me to get it to work

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

Also I had to rename class Google_HttpRequest in the Request.php to class Google_Http_Request becuase of the way autoload.php works, it splits the name up by "_".

Then just call it like so:

$req = new Google_Http_Request($reqUrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM