可以使用chrome.identity.launchWebAuthFlow来针对Google API进行身份验证吗? [英] Can chrome.identity.launchWebAuthFlow be used to authenticate against Google APIs?

查看:138
本文介绍了可以使用chrome.identity.launchWebAuthFlow来针对Google API进行身份验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a Chrome extension and have been trying to use chrome.identity.launchWebAuthFlow to authenticate with Google. I would prefer this to chrome.identity.getAuthToken (which does work) because getAuthToken gets the token for the user currently logged in to Chrome -- who may be logged in to multiple Google accounts. I want the user to be able to hook up a specific Google calendar to my extension, and that calendar might belong to a different user than they've logged in to Chrome as.

因此,我一直在尝试使用chrome.identity.launchWebAuthFlow进行此操作,并且通常会因不匹配的redirect_uri而失败.我已经尝试过几乎可以在Google API开发者控制台中设置的每种类型的凭据. ("Chrome应用程序"似乎是正确的选择,但我也尝试了Web应用程序,其他"和iOS.)我尝试使用chrome.extension.getURL('string')和chrome.app.getRedirectURL的结果(字符串")作为我的redirect_uri.

So, I've been trying to do this with chrome.identity.launchWebAuthFlow and generally failing around a mismatched redirect_uri. I've tried just about every type of credential you can set up in the Google APIs developer console. ("Chrome App" seemed like the right thing, but I have also tried Web application, Other, and iOS.) I've tried using the results of both chrome.extension.getURL('string') and chrome.app.getRedirectURL('string') as my redirect_uri.

我尝试了 https://stackoverflow.com/questions/40384255/所引用的示例应用程序oauth2-angular-chrome-extension ,但也无法使其正常工作.

I tried out the example app referred to by https://stackoverflow.com/questions/40384255/oauth2-angular-chrome-extension but have not been able to get that to work either.

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

I have a suspicion I'm trying to do something that either used to be allowed and no longer is, or just never worked.

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

Here's an example of my code, but I think my problem is really in the API dev console -- I don't see a way to set up a configuration that will work for an extension:

    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 端点.)

(I have also tried the https://accounts.google.com/o/oauth2/auth endpoint.)

解决方案:

在阅读了可接受的答案后,我想到了:

After reading the accepted answer, I wound up with this:

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返回了该参数,而不是将浏览器重定向到它-我可以从那里继续.

The responseUrl is my redirect_uri with parameters -- so Google oauth returned that instead of redirecting the browser to it -- and I could go on from there.

推荐答案

要获取

  • 在Google开发人员控制台中创建我自己的Web应用程序客户端ID,其授权重定向URI为https://bcgajjfnjjgadphgiodlifoaclnemcbk.chromiumapp.org/oauth2

    将该客户端ID复制到示例的config.json文件中.

    Copy that client ID into the config.json file of the sample.

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

    The call to get redirectURI in that sample is like chrome.identity.getRedirectURL("oauth2"), the string parameter gets appended to the end of the URL based on extension ID.

    这篇关于可以使用chrome.identity.launchWebAuthFlow来针对Google API进行身份验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆