Google Drive API不会上传文件

时间:2014-02-24 13:19:06

标签: php google-app-engine google-cloud-storage

我创建了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);

1 个答案:

答案 0 :(得分:0)

您忘记在$ authCode中输入身份验证代码 插入此代码:

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