How to use gmail api's in chrome extension?

浪尽此生 提交于 2019-12-12 09:08:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!