可以使用chrome.identity.launchWebAuthFlow对Google API进行身份验证吗?

时间:2016-11-03 21:25:58

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

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

所以,我一直在尝试使用chrome.identity.launchWebAuthFlow执行此操作,并且通常会在不匹配的redirect_uri中失败。我已尝试过您可以在Google API开发人员控制台中设置的所有类型的凭据。 (“Chrome App”似乎是正确的,但我也尝试过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返回了而不是将浏览器重定向到它 - 我可以从那里继续。

2 个答案:

答案 0 :(得分:0)

为了让Angular sample运行,我需要:

  1. 使用授权重定向URI https://bcgajjfnjjgadphgiodlifoaclnemcbk.chromiumapp.org/oauth2

  2. 在Google开发人员控制台中创建自己的Web应用程序客户端ID
  3. 将该客户端ID复制到样本的config.json文件中。

  4. 在该示例中获取redirectURI的调用与chrome.identity.getRedirectURL("oauth2")类似,字符串参数根据扩展ID附加到URL的末尾。

答案 1 :(得分:0)

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

manifest.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"
}

background.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);
});
相关问题