Google Picker API 自动

问题描述 投票:0回答:1

我计划有一个网络应用程序供用户将文件上传到我的驱动器。我创建一个服务帐户并获取 Google Picker 的 OAuth 令牌。我确认访问令牌有效。但是,Google Picker API 返回 403。仅当我使用我的帐户登录时才有效(“[email protected]”)。

我已经搜索了很多天,但仍然有解决方案。欢迎任何评论。

这是我的代码。

<?php

require_once '/vendor/autoload.php';
use Google\Client;
use Google\Service\Drive;

$client = new Client();
$client->setAuthConfig('service_account.json');

$client->setApplicationName("GooglePickerAPI");
$client->setScopes(['https://www.googleapis.com/auth/drive.file']);
$client->setSubject('[email protected]');
$client->setAccessType('offline');

$client->authorize()->get('https://www.googleapis.com/auth/drive.file');

// Get the access token
$accessToken = $client->getAccessToken();
$accessTokenJson = json_encode($accessToken);

?>

<!DOCTYPE html>
<html>
<head>
  <title>Picker API Quickstart</title>
  <meta charset="utf-8" />
</head>
<body>
<p>Picker API API Quickstart</p>

<!--Add buttons to upload-->
<button id="authorize_button" onclick="createPicker()">Upload</button>

<pre id="content" style="white-space: pre-wrap;"></pre>

<script type="text/javascript">
  let response = <?php echo $accessTokenJson; ?>

  const SCOPES = 'https://www.googleapis.com/auth/drive.file';
  const CLIENT_ID = 'MY_CLIENT_ID';
  const API_KEY = 'MY_API_KEY';
  const APP_ID = 'MY_APP_ID';

  let accessToken = response['access_token'];
  document.getElementById('authorize_button').style.visibility = 'visible';

  /**
   *  Create and render a Picker object for searching images.
   */
  function createPicker() {
    const view = new google.picker.View(google.picker.ViewId.DOCS);
    view.setMimeTypes('image/png,image/jpeg,image/jpg');
    const picker = new google.picker.PickerBuilder()
        .enableFeature(google.picker.Feature.NAV_HIDDEN)
        .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
        .setDeveloperKey(API_KEY)
        .setAppId(APP_ID)
        .setOAuthToken(accessToken)
        .addView(view)
        .addView(new google.picker.DocsUploadView())
        .setCallback(pickerCallback)
        .build();
    picker.setVisible(true);
  }

  /**
   * Displays the file details of the user's selection.
   * @param {object} data - Containers the user selection from the picker
   */
  async function pickerCallback(data) {
    if (data.action === google.picker.Action.PICKED) {
      let text = `Picker response: \n${JSON.stringify(data, null, 2)}\n`;
      const document = data[google.picker.Response.DOCUMENTS][0];
      const fileId = document[google.picker.Document.ID];
      console.log(fileId);
      const res = await gapi.client.drive.files.get({
        'fileId': fileId,
        'fields': '*',
      });
      text += `Drive API response for first document: \n${JSON.stringify(res.result, null, 2)}\n`;
      window.document.getElementById('content').innerText = text;
    }
  }
</script>
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
    <script>gapi.load("picker", "1");</script>    
</body>
</html>
google-picker
1个回答
0
投票

这似乎是最近才出现的问题,并且正在全球范围内发生。这可能与代码没有任何关系,它是谷歌本身的问题。请找到以下链接来跟踪此问题。 https://issuetracker.google.com/issues/328843663

© www.soinside.com 2019 - 2024. All rights reserved.