使用参数 `mine=true` 调用 activities:list 失败(Youtube 数据 API V3)

calling activities:list fails on using parameter `mine=true` (Youtube Data API V3)

我一直在尝试使用 API 检索我的活动,但我收到以下 JSON 错误。

{
    "error": {
        "code": 403,
        "message": "The request is not properly authorized.",
        "errors": [
            {
                "message": "The request is not properly authorized.",
                "domain": "youtube.activity",
                "reason": "forbidden"
            }
        ]
    }
}

,尽管我使用 https://www.googleapis.com/youtube/v3/activities?mine=true&key={my_api_key}&part=contentDetails 并且我使用 OAuth2 客户端获取访问令牌,我在调用 API.

时使用该访问令牌

我尝试使用这些示例,但我收到了同样的错误。

这是一个错误还是我做错了什么?


更多详情 我在邮递员中使用给定的 link 和 GET 方法,并使用 TYPE=OAuth2Prefix=Bearer

在令牌字段中放置一个有效的访问令牌

根据 Activities.list API 端点的官方规范,为了能够使用其 mine 请求参数,您必须在传递时发出对端点的调用正确的凭据:

mine (boolean)
This parameter can only be used in a properly authorized request. Set this parameter's value to true to retrieve a feed of the authenticated user's activities.

因此,使用 API 密钥是不够的(发出正确授权的请求时也不需要)。

请注意,从 API 获得的 JSON 错误响应与上面引用的规范完全一致。


根据 official (programming language agnostic) procedure,要从 API 获取有效的新访问令牌,请发出一个简单的 curl 实例,如下所示:

$ curl \
--data 'grant_type=refresh_token' \
--data-urlencode "client_id=$CLIENT_ID" \
--data-urlencode "client_secret=$CLIENT_SECRET" \
--data-urlencode "refresh_token=$REFRESH_TOKEN" \
https://oauth2.googleapis.com/token

以上,$CLIENT_ID$CLIENT_SECRET 是您从 Google 的开发人员控制台获得的客户端机密 JSON 文件的相应属性的值。 $REFRESH_TOKEN 是您在 运行 成功执行 OAuth2 authentication/authorization 流程后获得的(长期存在的)刷新令牌。

成功时从 curl 获得的输出如下所示:

{
  "access_token": "...",
  "expires_in": 3599,
  "scope": "...",
  "token_type": "Bearer"
}

使用 curlActivities.list 端点的调用是立即的:

$ curl \
--header "Authorization: Bearer $ACCESS_TOKEN" \
'https://www.googleapis.com/youtube/v3/activities?mine=true&part=contentDetails&maxResults=25'

上面的参数$ACCESS_TOKEN是您新获得的有效access token; curl 的输出看起来像:

{
  "kind": "youtube#activityListResponse",
  "etag": "...",
  "items": [
    {
      "kind": "youtube#activity",
      "etag": "...",
      "id": "...",
      "contentDetails": {
        ...
      }
    },
    ...
  ],
  "pageInfo": {
    "totalResults": ...,
    "resultsPerPage": 25
  }
}

对于 运行 CMD.exe 下的 Windows 机器上的上述 curl 命令——假设您已经替换了 $-变量自己手动 -- 替换 backslash character at the end of each line above with the caret character, ^. The percent character % should be doubled, i.e. should be replaced with %%, and the single quote characters ' should be replaced with double-quote characters ".