YouTube API v3-无法获得正确的频道ID

时间:2019-01-25 13:29:11

标签: javascript youtube-api youtube-data-api

在网络应用上登录oAuth2后,我需要获取一个用户频道ID,到目前为止,我是这样做的:

  var response = "";
  gapi.client.request({
    'method': 'GET',
    'path': 'https://www.googleapis.com/youtube/v3/channels',
    'params': {'part': 'snippet', 'mine': 'true', 'fields': fields},
    'scope': scope,
    'access_token': token
  }).execute(function(response) {
    if (response["items"][0]["id"] != null) {
      app.user.id = response["items"][0]["id"];
    } else {
      console.log("Error: " + response);
      // gapi error
    }
  });

但是,当我使用上面的代码并且用户注销并使用另一个帐户重新登录时,即使访问令牌与第二个帐户相关联,它仍然会返回该第一个帐户的频道ID。所以我想出了这个:

  var http = new XMLHttpRequest(),
  url = "https://www.googleapis.com/youtube/v3/channels",
  params = "part=id&mine=true&access_token=" + token;
  http.open("GET", url, true);
  http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
      if (http.responseText["items"][0]["id"] != null) {
        app.user.id = http.responseText["items"][0]["id"];
      } else {
        console.log("Error: " + http.responseText);
      }
    } else {
      console.log("Error: " + http.responseText);
    }
  }
  http.send(params);

谁总是给我这个回复:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Required parameter: part",
    "locationType": "parameter",
    "location": "part"
   }
  ],
  "code": 400,
  "message": "Required parameter: part"
 }
}

我不知道为什么...

0 个答案:

没有答案