2015-05-29 22 views
0

我正在使用Google Drive api將文件上傳到使用php的谷歌驅動器,但它每次都會詢問我的應用程序的身份驗證代碼。 我按照下面的網址給出的步驟 https://developers.google.com/drive/web/quickstart/quickstart-phpGoogle Drive API每次都要求驗證碼

我的代碼:

require_once 'src/Google/autoload.php'; 
$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('clientid'); 
$client->setClientSecret('clientsecret'); 
$client->setRedirectUri('url'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 
$service = new Google_Service_Drive($client); 
$authUrl = $client->createAuthUrl(); 

try{ 
    //Request authorization 
    print "Please visit:<br/>$authUrl<br/>\n"; 
    print "Please enter the auth code:\n"; 

// 
    $authCode = trim(fgets(STDIN)); 
// Exchange authorization code for access token 
$accessToken = $client->authenticate($authCode); 
$client->setAccessToken($accessToken); 
//Insert a file 
$file = new Google_Service_Drive_DriveFile(); 
$file->setTitle('document.txt'); 
$file->setDescription('A test document'); 
$file->setMimeType('text/plain'); 
$data = file_get_contents('document.txt'); 
$createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'text/plain', 
    )); 
print_r($createdFile); 
}catch(apiServiceException $e){ 
    echo $e->getMessage(); 
} 

響應:

https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=REDIRECT_URL&client_id=CLIENT_ID&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=online&approval_prompt=auto 
+0

你在調用API的代碼在哪裏?並請發佈回覆 – Raptor

+1

尋求調試幫助的問題(「爲什麼不是這個代碼工作?」)必須包括所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建最小,完整和可驗證示例。 – DaImTo

+0

我已更新我的問題 –

回答