Google 驱动 API 每次都要求验证码

Google Drive API every time asking for authentication code

我正在使用 google 驱动器 api 使用 php 将文件上传到 google 驱动器,但它每次都要求我提供应用程序的验证码. 我已按照下面 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

我已针对此问题进行了更多调试,最后我可以使用 PHP 将文件上传到 google 驱动器。 但是为此我们必须根据 google api 手动授权代码 然后我们将获得刷新令牌并使用该令牌我们可以将文件每次上传到 google 驱动器而无需询问任何密钥

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