authentication - chrome.identity.launchWebAuthFlow 能否用于针对 Google API 进行身份验证?

标签 authentication google-chrome-extension google-api oauth-2.0

我正在编写一个 Chrome 扩展程序,并一直在尝试使用 chrome.identity.launchWebAuthFlow 向 Google 进行身份验证。我更喜欢这个而不是 chrome.identity.getAuthToken (它确实有效),因为 getAuthToken 获取当前登录 Chrome 的用户的 token - 该用户可能登录到多个 Google 帐户。我希望用户能够将特定的 Google 日历连接到我的扩展程序,并且该日历可能属于与登录 Chrome 的用户不同的用户。

因此,我一直在尝试使用 chrome.identity.launchWebAuthFlow 来执行此操作,但通常会因重定向不匹配而失败。我已经尝试了您可以在 Google API 开发人员控制台中设置的几乎所有类型的凭据。 (“Chrome 应用程序”似乎是正确的,但我也尝试过 Web 应用程序、其他和 iOS。)我尝试使用 chrome.extension.getURL('string') 和 chrome.app.getRedirectURL 的结果('string') 作为我的redirect_uri。

我尝试了 https://stackoverflow.com/questions/40384255/oauth2-angular-chrome-extension 引用的示例应用程序但也未能使其发挥作用。

我怀疑我正在尝试做一些以前被允许但现在不再被允许的事情,或者只是从来没有成功过。

这是我的代码示例,但我认为我的问题实际上出在 API 开发控制台中 - 我没有找到一种方法来设置适用于扩展的配置:

    var auth_url = 'https://accounts.google.com/o/oauth2/v2/auth';
    var client_key = *[client id from API dev console]*
    var auth_params = { 
                        client_id: client_key,
                        redirect_uri: chrome.identity.getRedirectURL("oauth2.html")
                        scope: 'https://www.googleapis.com/auth/calendar'
                      };
    auth_url += '?' + $.param(auth_params);

    chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(token) { console.log(token); });

(我还尝试过 https://accounts.google.com/o/oauth2/auth 端点。)

解决方案:

阅读已接受的答案后,我得出以下结论:

var auth_url = 'https://accounts.google.com/o/oauth2/auth';
var client_id = '[client ID from console]';
var redirect_url = chrome.identity.getRedirectURL("oauth2.html");
var auth_params = {
    client_id: client_id,
    redirect_uri: redirect_url,
    response_type: 'token',
    scope: 'profile'
};
auth_url += '?' + $.param(auth_params);
console.log(auth_url);
chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(responseUrl) { console.log(responseUrl); });

responseUrl 是我的带有参数的redirect_uri——所以Google oauth 返回了它,而不是将浏览器重定向到它——我可以从那里继续。

最佳答案

是的,2019 年它仍然有效。终于成功了...

ma​​nifest.json

{
   "name": "Extension Name",
   "description": "Description",
   "version": "1.0.0",
   "manifest_version": 2,
   "icons": {
      "48": "icons/icon_48.png",
      "128": "icons/icon_128.png"
   },
   "background": {
      "scripts": [
         "background.js"
      ],
      "persistent": false
   },
   "oauth2": {
      "client_id": "Your Client ID from Google Develpers console (Must be Web Application)",
      "scopes": [
         "openid", "email", "profile"
      ]
   },
   "permissions": [
      "identity"
   ],
   "key": "Your Key from Google Developer Dashboard"
}

背景.js

chrome.windows.create({
    'url': './content/auth/auth.html',
    'width': 454,
    'height': 540,
    'type': 'popup'
});

auth.html

standard HTML markup that calls auth.js file

auth.js

var auth_url = 'https://accounts.google.com/o/oauth2/auth?';
var client_id = '<Client ID>';  // must be Web Application type
var redirect_url = chrome.identity.getRedirectURL(); // make sure to define Authorised redirect URIs in the Google Console such as https://<-your-extension-ID->.chromiumapp.org/

var auth_params = {
    client_id: client_id,
    redirect_uri: redirect_url,
    response_type: 'token',
    scope: 'https://mail.google.com/',
    login_hint: 'real_email@gmail.com' // fake or non-existent won't work
};

const url = new URLSearchParams(Object.entries(auth_params));
url.toString();
auth_url += url;

chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(responseUrl) { 
    console.log(responseUrl);
});

关于authentication - chrome.identity.launchWebAuthFlow 能否用于针对 Google API 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411493/

相关文章:

jquery - Rails Controller 方法在 Ajax 调用后不重新加载页面

javascript - AWS认知: How to assign user to a user group when user signs up

javascript - 在 JavaScript 中从 chrome.storage.local 中删除以字符串开头的变量

javascript - 创建一个简单的谷歌浏览器而不是右键单击所选文本导致所选文本变为斜体

javascript Chrome 扩展无法读取 httponly cookie

symfony - google calendar api 错误 400 - 资源 ID 值无效

android - 是否可以通过Google Places API获取 "Popular times"信息

android - 当用户从设置中删除 Android Authenticator 帐户时自动注销

c# - 通过 Google API 为新用户注册两步验证

authentication - 在AWS Cognito中,建议不要从浏览器调用/oauth2/token。为什么?