简体   繁体   English

Google Drive API没有上传文件

[英]Google Drive API does not upload a file

I have created API accent and used the following code, when I run this in a web browser, it asks me to authorise the App, which I do, and then no file is created in google drive. 我已经创建了API口音并使用了以下代码,当我在网络浏览器中运行此代码时,它要求我对应用进行授权,然后执行此操作,然后在Google驱动器中未创建任何文件。 Instead the browser redirects to my callback. 而是浏览器重定向到我的回调。 What am I doing wrong? 我究竟做错了什么?

   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);

You forgot to enter the authentication code in $authCode Insert this code : 您忘记在$ authCode中输入身份验证代码插入以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM