使用身份 launchWebAuthFlow 进行 google chrome 扩展时的 redirect_uri_mismatch

Jul*_*anJ 5 google-chrome-extension google-oauth

我一直在尝试让 chrome.identity.launchWebAuthFlow 与我的 Chrome 扩展程序一起使用以访问 Google 的 OAuth Flow(不,我不想使用 getAuthToken)。我遵循了这个问题中列出的实现。

这些是我进行设置的步骤:

  1. 为我添加到 manifest.json 的扩展生成了一个密钥,因此扩展 ID 不会更改(根据此答案
  2. 去谷歌云平台注册一个新的应用程序并启用API访问。对于凭据,我选择了 OAuth ID > Chrome App,输入了我的扩展 ID 并获得了我的客户端 ID。
  3. 我已经在我的 background.js 文件中实现了这段代码

    var auth_url = 'https://accounts.google.com/o/oauth2/auth';
    var client_id = '<client_id from Cloud Console>';
    var redirect_url = chrome.identity.getRedirectURL("oauth2");
    var auth_params = {
        client_id: client_id,
        redirect_uri: redirect_url,
        response_type: 'token',
        scope: 'profile'
    };
    
    var params = Object.keys(auth_params).map(function(k) {
        return encodeURIComponent(k) + '=' + encodeURIComponent(auth_params[k])}).join('&')
    auth_url += '?' + params;
    
    console.log(redirect_url);
    console.log(auth_url);
    chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, 
            function(responseUrl) { console.log(responseUrl); });
    
    Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到 Unchecked runtime.lastError while running identity.launchWebAuthFlow: Authorization page could not be loaded.

然后我在浏览器中检查身份验证 URL,错误是 400 redirect_uri_mismatch

现在我已经无数次检查了redirect_url。它的格式为 https://<app-id>.chromiumapp.org/oauth2- App ID 与我放入 Cloud Console Credentials 的内容完全匹配。当我重新加载扩展时,它也不会改变,因为它与 manifest.json 中的 Key 相关联

我错过了什么?任何人都可以确认这仍然有效吗?

作为参考,我的 manifest.json 文件

    {
      "name": "My OAuth Extension",
      "key": "<My KEY>",
      "version": "0.0.1",
      "manifest_version": 2,
      "description": "Testing OAuth",
      "homepage_url": "http://test.com",
      "default_locale": "en",
      "permissions": [
        "https://*/*",
        "identity",
        "*://*.google.com/*"
      ],
      "background": {
        "scripts": ["src/bg/background.js"],
        "persistent": true
      }
    }
Run Code Online (Sandbox Code Playgroud)

我错过了什么?任何人都可以确认这仍然有效吗?

小智 3

我的应用程序中有一个相同的项目并解决如下:

  1. 我在 Google Cloud Platform 中创建了一个 Web 应用程序类型的新 ClientID
  2. 我将以下 url 添加到授权重定向 URI:https://<appId>.chromiumapp.org/
  3. url我在函数的参数中使用了新的 ClientID chrome.identity.launchWebAuthFlow
  4. 登录后我进入redirect_url了函数回调。


归档时间:

查看次数:

387 次

最近记录:

6 年,10 月 前