问题
I am new to chrome extensions and gmail api's. I am trying to connect with gmail api's while implementation of my extension. I have api keys and client id.
回答1:
In your manifest you first need to add :
"background": {
"scripts": ["background.js" ,"client.js"]
},
"content_security_policy": "script-src https://*.google.com 'unsafe-eval'; object-src 'self'",
"oauth2": {
"client_id": "<your client id>",
"scopes": [
scope: 'https://mail.google.com/'
]
},
"permissions": [
"background",
"identity",
"tabs",
"https://www.googleapis.com/*",
"https://*.googleusercontent.com/*",
"https://mail.google.com/",
],
When client.js is the gmail API library ... Now in your background page you need to connect..I give you example in JS:
chrome.identity.getAuthToken(
{'interactive': true},
function(token){
/// you can use the token( user token ) for create http request to gmail api
}
);
window.gapi_onload = function(){
gapi.client.setApiKey("<your api code>");
gapi.auth.authorize(
{
client_id: '<Your client id>',
immediate: true,
scope: "https://mail.google.com/",
},
function(){
gapi.client.load('gmail', 'v1', gmailAPILoaded);
}
);
}
function gmailAPILoaded(){
// Do things you want with gapi.client
}
Most of things I give you based on this guide but it's not work for me exectly so I change some things.
Good luck
回答2:
You do not necessarily need the gapi.auth.authorize(...)
calls. Look for the chrome.identity
API instead.
来源:https://stackoverflow.com/questions/41118712/how-to-use-gmail-apis-in-chrome-extension