简体   繁体   English

具有域范围委托的 Google 日历 API:events.watch 方法失败并显示“请求缺少有效的 API 密钥”

[英]Google Calendar API with domain wide delegation: events.watch method fails with "The request is missing a valid API key"

Background背景

I have followed all of the steps for domain wide delegation.我已遵循域范围授权的所有步骤。 An example of this is the ability to sign in with the service account json, impersonate users in my Google Workspace account and list their events (see example below).这方面的一个示例是能够使用服务帐户 json 登录,在我的 Google Workspace 帐户中模拟用户并列出他们的事件(参见下面的示例)。

# list-events.py
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint

def create_calendar_api_service(service_acc_filepath):
    """Build and return a Google Calendar service object authorized with the service
    account that acts on behalf of a given user.

    Args:
        service_acc_filepath: path to service account json with domain wide delegation enabled.
    """
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        service_acc_filepath, scopes=str(SCOPES)
    )
    # impersonate user with emailaddress=calendarId
    delegate = credentials.create_delegated(USER_EMAIL)
    return build("calendar", "v3", credentials=delegate)


if __name__ == "__main__":
    UUID = get_uuid()
    SCOPES = "https://www.googleapis.com/auth/calendar"
    SERVICE_ACCOUNT_FILE_PATH = "./workspace-sa/my-project-d2355248f03f.json"
    USER_EMAIL = "workspace-user-1@company.com"
    BASE_URL = "https://acf1-92-233-162-75.eu.ngrok.io"

    
    # get authenticated object to interact with Google API.
    my_service = create_query_api_service(SERVICE_ACCOUNT_FILE_PATH)

    req = my_service.events().list(calendarId=f"{USER_EMAIL}").execute()

The problem I am having is that the same setup doesn't let me receive notifications from the watch() method...我遇到的问题是相同的设置不允许我从 watch() 方法接收通知...

# watch-for-events.py
...
body = {
        "id": f"{UUID}",
        "type": "webhook",
        "address": f"{BASE_URL}/calendar-push",  # The URL to which notifications are delivered for this channel.
    }

my_service = create_calendar_api_service(SERVICE_ACCOUNT_FILE_PATH)

req = my_service.events().watch(calendarId=f"{USER_EMAIL}", body=body).execute()

Running the watch() method returns the following to the terminal.运行 watch() 方法将以下内容返回到终端。

{'expiration': '1669901130000',
 'id': '0184a9d0-8b55-c62d-ee15-84891fcf1014',
 'kind': 'api#channel',
 'resourceId': '7kxHSfF9AbNVLSXk5cxPcmgfhAg',
 'resourceUri': 'https://www.googleapis.com/calendar/v3/calendars/workspace-user-1%40company.com/events?alt=json'}

Upon following the resourceUri I see the aforementioned error message.在遵循 resourceUri 后,我看到了上述错误消息。

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

The Main Problem主要问题

very single change in the target calendar hits my https webhook/endpoint with a 400 BAD REQUEST .目标日历中的非常单一的更改会以400 BAD REQUEST命中我的 https webhook/端点。

I cannot even read the request in my Flask backend.我什至无法在我的 Flask 后端读取请求。

Question问题

Can someone please help me understand why I am not able to receive push notifications (why they all return 400 BAD REQUEST)?有人可以帮我理解为什么我无法收到推送通知(为什么他们都返回 400 BAD REQUEST)?

PS: I have read that I can provide some further access credentials via a bearer token in the header. But there is no header attribute in these methods. PS:我读到我可以通过 header 中的不记名令牌提供一些进一步的访问凭证。但是这些方法中没有 header 属性。

Here are the images below for the workspace admin settings in question:以下是相关工作区管理员设置的图片: 我们在 ServiceAccountCredentials() 方法中使用其 json 的服务帐户所允许的范围

Endpoint code端点代码

@app.route("/calendar-push", methods=["GET", "POST"])
def add_message():
    app.logger.info("HIT THE /calendar_push ROUTE")
    content = request.json
    app.logger.info(content)
    return request.json

Output from flask endpoint after moving calendar events移动日历事件后来自 flask 端点的 Output

127.0.0.1 - - [24/Nov/2022 17:39:50] "POST /calendar-push HTTP/1.1" 400 -

'resourceUri': 'https://www.googleapis.com/calendar/v3/calendars/workspace-user-1%40company.com/events?alt=json' 'resourceUri':'https://www.googleapis.com/calendar/v3/calendars/workspace-user-1%40company.com/events?alt=json'

All that is is telling you what notification endpoint the message response is for.所有这些都是告诉您消息响应是针对哪个通知端点的。

In which case you will need to do a在这种情况下,您需要做一个

events = service.events().list(calendarId='workspace-user-1%40company.com').execute()

Your getting missing api key because your calling the endpoint without first being authorized.您缺少 api 密钥,因为您在未经授权的情况下调用端点。

Authorize the call then check it and you will get a response.授权调用然后检查它,您将得到响应。

暂无
暂无

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

相关问题 该请求缺少有效的 API 密钥 Google 搜索 API python - The request is missing a valid API key Google search API python 当向 Vision API 产品搜索发出请求时,出现错误“消息”:“请求缺少有效的 API 密钥。” - When making a request to the Vision API Product Search an error occurs "message": "The request is missing a valid API key." com.google.firebase.FirebaseException:发生内部错误。 [ API 密钥无效。 请传递有效的 API 密钥。 ] - com.google.firebase.FirebaseException: An internal error has occurred. [ API key not valid. Please pass a valid API key. ] 由于缺少 api 代理,从 Google Appengine 发送电子邮件失败 - Sending emails from Google Appengine fails because of missing api proxy 使用 google 日历 API 创建活动时未添加 Google Meet 链接 - google meet link not getting added while creating events using google calendar API POST 到 api 网关时无法解析“授权标头中的无效键 = 值对(缺少等号)” - Unable to resolve " not a valid key=value pair (missing equal-sign) in Authorization header" when POSTing to api gateway 谷歌云(API 网关)自定义域 - Google Cloud (API GATEWAY) Custom Domain 未捕获(承诺)FirebaseError:Firebase:错误(auth/api-key-not-valid.-please-pass-a-valid-api-key.)(Netlify) - Uncaught (in promise) FirebaseError: Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.) (Netlify) 用于本地测试的 Google API 密钥 - Google API Key for Local Testing Firebase:错误(auth/api-key-not-valid.-please-pass-a-valid-api-key.) - Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM