Using Google Calendar API returns 401 (unauthorized)

断了今生、忘了曾经 提交于 2019-12-13 14:41:56

问题


I'm trying to use Google Calendar API via JavaScript and keep getting the 401 error response while using the following code:

$.ajax({ 
   dataType: "json", 
   url: "https://www.googleapis.com/calendar/v3/users/me/calendarList/<calendarID>?key=<my api key>", 
   success: function(result){ 
   $('#result').html(result); 
   } 
});

Is there something wrong in my usage of the API and how should I use the oAuth? I already have a clientID and secretID from the Google console but don't know how to use those.

Thanks.


回答1:


Here's the answer after quite a bit of research, I just found and used this guide for using google calendar api http://googleappsdeveloper.blogspot.com/2011/12/using-new-js-library-to-unlock-power-of.html

Then I still was confused since the api key wasn't working every I tried regenerating it a couple of times. Then I had to use the browser key without any referrers just as mentioned here: Getting Error 403: Access Not Configured. Please use Google Developers Console to activate the API for your project

and finally I got everything working.

Here's also a main article for understanding the OAuth2 https://developers.google.com/accounts/docs/OAuth2




回答2:


In order to use Google Calendar API, you not only need the client ID and client secret but in most cases (other than accessing public calendars) you also need to have your user authenticated. One way to achieve that is using Oauth2.

I recommend using the client library that you can download here: https://developers.google.com/google-apps/calendar/downloads

Also there is an example on how to use the library here: https://developers.google.com/api-client-library/javascript/samples/samples



来源:https://stackoverflow.com/questions/25558752/using-google-calendar-api-returns-401-unauthorized

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