zend gdata and google spreadsheet not connecting

后端 未结 5 1105
醉酒成梦
醉酒成梦 2020-12-09 12:58

ive been using Zend Gdata for a while now, and today im getting an error of

Notice: Undefined offset: ClientLogin.php on line 150

via php,

5条回答
  •  青春惊慌失措
    2020-12-09 13:46

    I uses ClientLogin for a server to server application.

    needed to switch to oAuth2 in a hurry today. I merged some examples I found to get the authorisation token using a 'Service account'

    function get_token() {
    $client_email = '0-1.apps.googleusercontent.com';
    $client_email = '0-1@developer.gserviceaccount.com';
    $private_key = file_get_contents('abc.p12');
    $scopes = array('https://spreadsheets.google.com/feeds');
    $credentials = new Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
        'notasecret',                                 // Default P12 password
        'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type
    );
    
    $client = new Google_Client();
    $client->setAssertionCredentials($credentials);
    if ($client->getAuth()->isAccessTokenExpired()) {
     $client->getAuth()->refreshTokenWithAssertion();
    }
     $tokenData = json_decode($client->getAccessToken());
            return $tokenData->access_token;
    }
    

    I needed to use the developer email not the app email, same email must be added as a user to the spreadsheet

    the generated token can then be used with php-google-spreadsheet-client

    $accessToken = get_token();
    
    $serviceRequest = new DefaultServiceRequest($accessToken);
    ServiceRequestFactory::setInstance($serviceRequest);
    
    $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
    $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
    
    $spreadsheet = $spreadsheetFeed->getByTitle('Hello World');
    

提交回复
热议问题