2014-02-24 155 views
1

我已經創建了API口音並使用了以下代碼,當我在Web瀏覽器中運行此代碼時,它要求我授權該應用程序,並執行此操作,然後不會創建任何文件谷歌驅動器。相反,瀏覽器重定向到我的回調。我究竟做錯了什麼?Google Drive API不會上傳文件

require_once 'google-api-php-client/src/Google_Client.php'; 
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

    $client = new Google_Client(); 
    // Get your credentials from the console 
    $client->setClientId('myid'); 
    $client->setClientSecret('mysecret'); 
    $client->setRedirectUri('http://mydomain.com/oauth2callback'); 
    $client->setScopes(array('https://www.googleapis.com/auth/drive')); 

    $service = new Google_DriveService($client); 

    $authUrl = $client->createAuthUrl(); 

    //Request authorization 


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

    //Insert a file 
    $file = new Google_DriveFile(); 
    $file->setTitle('My document'); 
    $file->setDescription('A test document'); 
    $file->setMimeType('text/plain'); 

    $data = "Hello World"; 

    $createdFile = $service->files->insert($file, array(
    'data' => $data, 
    'mimeType' => 'text/plain', 
)); 

    print_r($createdFile); 

回答

0

你忘了在$ AUTHCODE 插入輸入驗證碼驗證碼:

$authcode = 'xxxxxxxxxx' // insert your authentication code that you get from your browser 
$accessToken = $client->authenticate($authCode); 
相關問題