首页 > 解决方案 > Google Drive Watch 资源 URI 返回 403

问题描述

我有点卡住了。我通过服务帐户连接到 google drive 客户端,我可以查看 + 下载资源。

我的代码:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import httplib2
import os
import io
import uuid

def watch_file(service, file_id, channel_id, channel_type, channel_address):
    """Watch for all changes to a user's Drive.

    Args:
    service: Drive API service instance.
    file_id: ID of the file to watch.
    channel_id: Unique string that identifies this channel.
    channel_type: Type of delivery mechanism used for this channel.
    channel_address: Address where notifications are delivered.

    Returns:
    The created channel if successful, None otherwise.
    """
    body = {
    'id': channel_id,
    'type': channel_type,
    'address': channel_address
    }
    try:
        return service.files().watch(fileId=file_id, body=body).execute()
    except ValueError as error:
        print ('An error occurred: %s' % error)
    return None


scopes = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('test-service-account-project-294621-23bc75e9e4fb.json', scopes)

http_auth = credentials.authorize(Http())
drive = build('drive', 'v3', http=http_auth)

channel_id = str(uuid.uuid4())
channel_type = 'web_hook'
channel_address = '/my/remote/server/address'

watch_file(drive, '{resource-id}', channel_id, channel_type, channel_address)

当我尝试访问watch特定资源时,请求通过并得到以下响应:

{
   "kind":"api#channel",
   "id":"b7fef95a-194a-4ba9-befb-8ac19bcdb8e3",
   "resourceId":"PxMhSDUDVo1YKsVHkC0JGUKqpz8",
   "resourceUri":"https://www.googleapis.com/drive/v3/files/{resource-id}?acknowledgeAbuse=false&alt=json&supportsAllDrives=false&supportsTeamDrives=false&alt=json",
   "expiration":"1604634553000"
}

据我了解,上面的响应片段看起来是正确的,但是当我resourceId在浏览器中搜索 uri 时,会看到以下响应:

{
   "error":{
      "errors":[
         {
            "domain":"usageLimits",
            "reason":"dailyLimitExceededUnreg",
            "message":"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
            "extendedHelp":"https://code.google.com/apis/console"
         }
      ],
      "code":403,
      "message":"Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
   }
}

请注意,我有一台服务器正在运行,并watch配置为已部署的服务器端点。我可以看到请求通过,但手表正文是空的,resourceUri 包含 403。这闻起来很奇怪,而且令人困惑,因为我的客户端配置正确,即我正在通过 API 发出请求,但我收到Daily Limit了问题。就好像watchPOST 需要传入一些其他身份验证(我尝试添加 API 密钥但没有运气)。

谢谢

编辑: 我对此感兴趣,resourceUri因为我认为还有更多信息需要解压。话虽如此,在阅读了有关通知预期输出的更多信息后,我认为我看到的通知响应是正确的,即标头+没有正文-但我认为我不应该在 resourceUri 中看到 403。

来自服务器的通知响应


2020-11-06T14:11:56.760215+00:00 app[web.1]: HEADERS: Connection: close
2020-11-06T14:11:56.760216+00:00 app[web.1]: X-Goog-Channel-Expiration: Fri, 06 Nov 2020 15:04:03 GMT
2020-11-06T14:11:56.760219+00:00 app[web.1]: User-Agent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
2020-11-06T14:11:56.760220+00:00 app[web.1]: Content-Length: 0
2020-11-06T14:11:56.760220+00:00 app[web.1]: Host: {host}
2020-11-06T14:11:56.760221+00:00 app[web.1]: Accept-Encoding: gzip,deflate,br
2020-11-06T14:11:56.760221+00:00 app[web.1]: Accept: */*
2020-11-06T14:11:56.760222+00:00 app[web.1]: X-Forwarded-Proto: https
2020-11-06T14:11:56.760222+00:00 app[web.1]: Total-Route-Time: 0
2020-11-06T14:11:56.760223+00:00 app[web.1]: X-Goog-Resource-Id: {resource-id}
2020-11-06T14:11:56.760223+00:00 app[web.1]: X-Forwarded-Port: 443
2020-11-06T14:11:56.760223+00:00 app[web.1]: X-Forwarded-For: 66.249.88.175
2020-11-06T14:11:56.760225+00:00 app[web.1]: X-Goog-Resource-Uri: {resource-uri}
2020-11-06T14:11:56.760225+00:00 app[web.1]: X-Goog-Message-Number: 473340
2020-11-06T14:11:56.760226+00:00 app[web.1]: Connect-Time: 0
2020-11-06T14:11:56.760226+00:00 app[web.1]: X-Goog-Resource-State: update
2020-11-06T14:11:56.760226+00:00 app[web.1]: X-Request-Id: {request-id}
2020-11-06T14:11:56.760227+00:00 app[web.1]: Via: 1.1 vegur
2020-11-06T14:11:56.760227+00:00 app[web.1]: X-Goog-Channel-Id: {channel-id}
2020-11-06T14:11:56.760227+00:00 app[web.1]: X-Goog-Changed: children
2020-11-06T14:11:56.760228+00:00 app[web.1]: X-Request-Start: 1604671916756
2020-11-06T14:11:56.760228+00:00 app[web.1]: 
2020-11-06T14:11:56.760229+00:00 app[web.1]: 
2020-11-06T14:11:56.760229+00:00 app[web.1]: BODY: None

标签: pythongoogle-drive-api

解决方案


推荐阅读