【问题标题】:Copy google doc spreadsheet using php api使用 php api 复制 google doc 电子表格
【发布时间】:2014-12-03 12:14:09
【问题描述】:

我想复制 Google Doc 电子表格文件。我正在使用这个 code

function copyFile($service, $originFileId, $copyTitle) {
  $copiedFile = new Google_DriveFile();
  $copiedFile->setTitle($copyTitle);
  try {
    return $service->files->copy($originFileId, $copiedFile);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}


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('myclientid');
$client->setClientSecret('myclient secret');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);



copyFile($service,'my schema id','Copy Of Schema');

我无法获取 $service 实例。所以我搜索并获得了上述方法,但现在它给出了 401 login required 错误。

请帮帮我

【问题讨论】:

    标签: php excel copy google-docs google-api-php-client


    【解决方案1】:

    您需要检查用户是否有足够的授权

    1. 您需要生成授权网址:

    $authUrl = $client->createAuthUrl();

    1. 将用户重定向到 authUrl

    2. 让用户粘贴授权码:

    $authCode = trim(fgets(STDIN));
    
    1. 认证

    $accessToken = $client->authenticate($authCode);

    $client->setAccessToken($accessToken);

    1. 完成上述操作后,您现在已使用 OAuth 对用户进行身份验证以访问驱动器。

    参考:Click here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      相关资源
      最近更新 更多