YouTube API v3 JavaScript-OAuth无法正常运行

时间:2020-06-02 17:10:30

标签: javascript api oauth youtube

我正在研究如何使用YT API v3进行简单搜索,然后看了https://github.com/youtube/api-samples/tree/master/javascript,然后使用ExpressJS(在GCP中已正确授权localhost:5000)进行了试射,并创建了API密钥和GCP上的OAuth凭据,但是当我运行测试时,执行以下功能时出现了一个奇怪的错误:

// Handle the result of a gapi.auth.authorize() call.
function handleAuthResult(authResult) {
  //Here I printed the authResult to see what was going on
  console.log(authResult)
  if (authResult && !authResult.error) {
    // Authorization was successful. Hide authorization prompts and show
    // content that should be visible after authorization succeeds.
    $('.pre-auth').hide();
    $('.post-auth').show();
    loadAPIClientInterfaces();
  } else {
    // Make the #login-link clickable. Attempt a non-immediate OAuth 2.0
    // client flow. The current function is called when that flow completes.
    $('#login-link').click(function() {
      gapi.auth.authorize({
        client_id: OAUTH2_CLIENT_ID,
        scope: OAUTH2_SCOPES,
        immediate: false
        }, handleAuthResult);
    });
  }
}

当我打印变量authResult时,它给了我以下对象:

>{
  "error": "immediate_failed",
  "error_subtype": "no_user_bound",
  {
  "error": "immediate_failed",
  "error_subtype": "no_user_bound",
  "status": {
    "signed_in": false,
    "method": null,
    "google_logged_in": false
  },
  "client_id": "MY_CLIENT_ID",
  "cookie_policy": "single_host_origin"
}

在“ MY_CLIENT_ID”中,放入我的OAuth客户端ID。我真的不知道该如何解决该问题,我在整个stackoverflow中进行了搜索,发现的最接近的东西是gapi.client.youtube is undefined?

1 个答案:

答案 0 :(得分:0)

它尝试使用immediate: true在页面加载时进行身份验证,但是该页面尚无权限,因此会出现错误。如果单击#login-link元素,则应显示oauth流以允许权限。然后,在随后的页面重新加载中,错误应该不会出现。

相关问题