所以基本上,我正在使用 vanilla javascript for Outlook 构建一个 Chrome 扩展。在该扩展中,我必须获取当前登录用户的电子邮件。我已在 Azure 上注册了我的应用程序,获取了客户端 ID,在应用程序中添加了必要的权限,如 openId 、 profile 、 User.Read 、 User.Mail ,添加了必要的范围,但我仍然没有获得身份令牌。
**
manifest.json**
{
"manifest_version": 3,
"name": "Plugin for Outlook",
"description": "Base Level Extension",
"version": "1.0",
"action": {
"default_popup": "popup/popup.html",
"default_icon": "images/logo.png"
},
"permissions": [
"contextMenus",
"activeTab",
"scripting",
"storage",
"identity"
],
"host_permissions": [
"https://graph.microsoft.com/*",
"https://ccqrs034g7.execute-api.us-east-1.amazonaws.com/*"
],
"background": {
"service_worker": "background.js"
},
"oauth2": {
"client_id":"my id",
"scopes": [
"openid", "email", "profile" , "https://graph.microsoft.com/User.Read"
]
},
"content_scripts":[
{
"matches": ["<all_urls>"],
"js":["contentScript.js"]
}
]
}
背景.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("hello");
if (message.action === "getEmail") {
chrome.identity.getAuthToken({ interactive: true }, function (token) {
if (chrome.runtime.lastError || !token) {
console.error(chrome.runtime.lastError);
return;
}
fetch("https://graph.microsoft.com/v1.0/me", {
headers: {
Authorization: "Bearer " + token,
},
})
.then((response) => response.json())
.then((userInfo) => {
const data = message.data;
console.log(data);
// Sending Data to backend
})
.catch((error) => console.error(error));
});
}
return true;
});
发生错误的原因是 Google Chrome 无法理解您在
scope
中指定的内容。 oauth2
中的manifest.json
键用于通过Google对用户进行身份验证。您可以在这个官方指南中阅读更多相关信息。
chrome.identity.getAuthToken,也旨在从 Google 获取令牌。要在非 Google 服务中实现身份验证,您需要使用 chrome.identity.launchWebAuthFlow。
以下是非 Google 帐户身份验证的工作原理。