Google Drive API每次都要求验证码

时间:2015-05-29 08:37:37

标签: php curl google-drive-api

我正在使用谷歌驱动器api使用PHP将文件上传到谷歌驱动器,但它每次都要求我提供我的应用程序的身份验证代码。 我已按照以下URL中给出的步骤操作 https://developers.google.com/drive/web/quickstart/quickstart-php

我的代码:

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

1 个答案:

答案 0 :(得分:0)

我已针对此问题进行了更多调试,最后我可以使用PHP将文件上传到Google云端硬盘。 但为此我们必须根据谷歌api手动授权代码 然后我们将获得刷新令牌并使用该令牌我们可以在每次询问任何键时将文件上传到谷歌驱动器

我在这里添加了代码https://rgknowledgebase.wordpress.com/2015/06/05/php-upload-file-to-google-drive/