Uncaught exception 'Google_Auth_Exception' with message 'Invalid code'

只愿长相守 提交于 2019-12-24 21:30:34

问题


i'm tring to migrate my google calendar access from Zend to new google API since they closed the service in november. My web app uses google api to create some events.

I'm facing a recurring message that i could not resolve : Uncaught exception 'Google_Auth_Exception' with message 'Invalid code'

Here's my code :

define('STDIN',fopen("php://stdin","r"));
require_once '../../utils/google-api-php-client-master/autoload.php';

    /**********************
    OAUTH 2.0 AUTHORIZATION
    ***********************/


    $client = new Google_Client();

    // OAuth2 client ID and secret can be found in the Google Developers Console.
    $client->setClientId('XXXXXX);
    $client->setClientSecret('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    $client->setRedirectUri('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    $client->addScope('https://www.googleapis.com/auth/calendar');

    $service = new Google_Service_Calendar($client);

    $authUrl = $client->createAuthUrl();


    //Request authorization
    print "Please visit:\n$authUrl\n\n";
    print "Please enter the auth code:";
    echo(trim(fgets(STDIN)));
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for access token
    $accessToken = $client->authenticate($authCode);
    $client->setAccessToken($accessToken); 

Could someone please help me ?


回答1:


I finally got this stuff to start working myself and a lot of searching. I was also using Zend before. There is a very good website at Daimto.com where you can see a bunch of tutorials. Here is the code that worked for me to add an event using the code form Daimto.com and adding code for adding an event in the body. Remember you need ot have the service email added to the share of your google calendar too!

<?php
session_start();        
require_once './google-api-php-client/src/Google/Client.php';
require_once './google-api-php-client/src/Google/Service/Calendar.php';     

$client_id = '6846057_YOUR_CLIENT_ID_HERE_pg3q8r6.apps.googleusercontent.com';
$Email_address = '68460_YOUR_SERVICE_EMAIL_HERE_developer.gserviceaccount.com';  
$key_file_location = '_KEY_FILE_LOCATION_HERE_8.p12';       
$client = new Google_Client();      
$client->setApplicationName("_APP_NAME_HERE_");
$key = file_get_contents($key_file_location);    
// seproate additional scopes with a comma   
$scopes ="https://www.googleapis.com/auth/calendar";    
$cred = new Google_Auth_AssertionCredentials(    
   $Email_address,       
    array($scopes),     
    $key         
    );      
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
    $client->getAuth()->refreshTokenWithAssertion($cred);       
}       
$service = new Google_Service_Calendar($client);    

?>

<html><body>

<?php
    //$service = new Google_Service_Calendar($client);
    //
    $event = new Google_Service_Calendar_Event();
    $event->setSummary('Event 2');
    $event->setLocation('Somewhere');
    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime('2015-06-22T19:00:00.000+01:00');
    $start->setTimeZone('Europe/London');
    $event->setStart($start);
    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime('2015-06-22T19:25:00.000+01:00');
    $end->setTimeZone('Europe/London');
    $event->setEnd($end);
    //
     $calendar_id = "nm_GOOGLE_CAL_ID_HERE_@group.calendar.google.com";
    //
    $new_event = null;
    //
    try {
        $new_event = $service->events->insert($calendar_id, $event);
        //
        $new_event_id= $new_event->getId();
    } catch (Google_ServiceException $e) {
        syslog(LOG_ERR, $e->getMessage());
    }
    //
    $event = $service->events->get($calendar_id, $new_event->getId());
    //
    if ($event != null) {
        echo "Inserted:";
        echo "EventID=".$event->getId();
        echo "Summary=".$event->getSummary();
        echo "Status=".$event->getStatus();
    }    

?>


来源:https://stackoverflow.com/questions/27266663/uncaught-exception-google-auth-exception-with-message-invalid-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!