我正在编写一个 Chrome 扩展程序,并一直在尝试使用 chrome.identity.launchWebAuthFlow 与 Google 进行身份验证。我更喜欢 chrome.identity.getAuthToken (它确实有效),因为 getAuthToken 获取当前登录到 Chrome 的用户的令牌——他们可能登录到多个 Google 帐户。我希望用户能够将特定的 Google 日历连接到我的扩展程序,并且该日历可能属于与他们登录 Chrome 不同的用户。
所以,我一直在尝试使用 chrome.identity.launchWebAuthFlow 来做到这一点,但通常会在不匹配的 redirect_uri 问题上失败。我已经尝试了您可以在 Google API 开发人员控制台中设置的几乎所有类型的凭据。(“Chrome 应用程序”似乎是正确的,但我也尝试过 Web 应用程序、其他和 iOS。)我尝试使用 chrome.extension.getURL('string') 和 chrome.app.getRedirectURL 的结果('string') 作为我的 redirect_uri。
我尝试了/sf/ask/2826897881/引用的示例应用程序,但也无法使其正常工作。
我怀疑我正在尝试做一些曾经被允许但不再允许的事情,或者只是从未奏效。
这是我的代码示例,但我认为我的问题确实出在 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); …Run Code Online (Sandbox Code Playgroud)