【问题标题】:YouTube API v3 javascript - OAuth not workingYouTube API v3 javascript - OAuth 不起作用
【发布时间】:2020-09-21 05:52:41
【问题描述】:

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

// 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?

【问题讨论】:

    标签: javascript api oauth youtube


    【解决方案1】:

    它尝试在页面加载时使用immediate: true 进行身份验证,但该页面还没有权限,因此出现错误。如果您单击 #login-link 元素,它应该会调出 oauth 流以允许权限。然后,在后续页面重新加载时,该错误不应出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 2012-04-20
      • 2017-05-04
      • 2019-08-07
      • 2015-06-22
      • 2013-09-05
      • 2019-08-07
      • 2013-11-28
      相关资源
      最近更新 更多