【问题标题】:Validating google oauth tokens (youtube.readnonly)验证 google oauth 令牌 (youtube.readnonly)
【发布时间】:2021-04-03 15:35:15
【问题描述】:

我正在制作一个不需要使用 youtube 帐户信息(显示在频道链接中的 ID)的 chrome 扩展程序。我已经解决了让chrome.identity.getAuthToken 给我一个带有"scopes":["https://www.googleapis.com/auth/youtube.readonly"] 的令牌。它看起来像这样:ya29.a0AfH6SMDRpsZf***48VT_yQ4FCsFc8。现在我有一个问题:如何使用此令牌获取帐户信息?我必须发送什么请求才能做到这一点?

我已经尝试过这里的方法:https://developers.google.com/youtube/v3/guides/auth/installed-apps

POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob%3Aauto&
grant_type=authorization_code

但我在developer console 中的chrome oauth2 app 甚至没有client_secret,所以看起来不像我的方式。

getAuthToken() tutorial (https://developer.chrome.com/docs/extensions/mv2/tut_oauth/) 中,他们使用 google people API 并以这种方式对其进行验证:

let init = {
        method: 'GET',
        async: true,
        headers: {
          Authorization: 'Bearer ' + token,
          'Content-Type': 'application/json'
        },
        'contentType': 'json'
      };
      fetch(
          'https://people.googleapis.com/v1/contactGroups/all?maxMembers=20&key=<API_Key_Here>',
          init)
          .then((response) => response.json())
          .then(function(data) {
            console.log(data)
          });

如果我将范围更改为 contacts.readonly 但我需要 youtube.readonly 并且我不明白我必须在哪里发送类似的请求来获取我需要的信息,它对我有用。有人可以帮忙吗?

【问题讨论】:

    标签: javascript google-chrome-extension google-api youtube-api youtube-data-api


    【解决方案1】:

    尝试使用范围内的“配置文件”以及 youtube 数据 api 点。

    在我的 chrome 扩展程序中,我使用 gapi 来获取信息,这包括登录和 youtube 数据 api,用于处理 youtube 视频。这将返回 getBasicProfile 中的用户信息和 getAuthResponse 中的身份验证信息。

    我的范围内有以下内容 范围:“电子邮件配置文件https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/youtubeopenid https://www.googleapis.com/auth/userinfo.profile"

    注意:您不应使用 JS 文件中的客户端密码。配置应仅包含客户端 ID、apiKey、范围和重定向 ui(根据您的情况)。

    【讨论】:

      猜你喜欢
      • 2014-12-24
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多