2016-07-08 3 views
-1

Я хочу показать свой Google-диск на веб-сайте. Это означает, что если кто-либо использует мой сайт, вы можете видеть файлы и папку с файлами google без входа в систему. Вот какой код я пытаюсь реализовать.Интеграция с Google Диском Api с php

include_once 'google-api-php-client/src/Google/autoload.php'; 
    $scopes = array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.appdata', 
    'https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.metadata','https://www.googleapis.com/auth/drive.metadata.readonly','https://www.googleapis.com/auth/drive.photos.readonly','https://www.googleapis.com/auth/drive.readonly'); 

    /** 
    * Create AssertionCredentails object for use with Google_Client 
    */ 
    $creds = new Google_Auth_AssertionCredentials(
     $serviceAccountName, 
     $scopes, 
     file_get_contents($keyFile) 
    ); 

    $creds->sub = $delegatedAdmin; 

    /** 
    * Create Google_Client for making API calls with 
    */ 
    $client = new Google_Client(); 
    $client->setApplicationName($appName); 
    $client->setClientId($clientId); 
    $client->setAssertionCredentials($creds); 

    /** 
    * Get an instance of the Directory object for making Directory API related calls 
    */ 
    $service = new Google_Service_Drive($client); 

    $optParams = array(
     'pageSize' => 10, 
     'fields' => "nextPageToken, files(id, name)" 
    ); 
    $results = $service->files->listFiles($optParams); 
    print_r($results); exit; 
    /** 

Может ли кто-нибудь связаться со мной, как это сделать. Я получаю эту ошибку

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "unauthorized_client", "error_description" : "Unauthorized client or scope in request." } 

ответ

0

Вам нужно добавить

if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion(); 
} 

после

$client->setAssertionCredentials($creds); 
+0

Фатальная ошибка: Uncaught исключение 'Google_Auth_Exception' с сообщением 'Ошибка при обновлении маркера OAuth2, сообщение:' {«error»: «unauthorized_client», «error_description»: «Неавторизованный клиент или область в запросе». } '' –

+0

По-прежнему получать ту же ошибку –

+0

Попробуйте удалить '$ creds-> sub = $ delegatedAdmin;' –

Смежные вопросы