0

I am trying to make ajax call to blogger.com . The call retrives the data of the user . I am using this https://developers.google.com/blogger/docs/3.0/ as reference . The following is the ajax I wrote .

var getUserDetail = function(){

var url = "https://www.googleapis.com/blogger/v3/users/self";

$.ajax({
    url: url,
type: 'GET',
beforeSend: function (request)
    {

        request.setRequestHeader("Authorization","oauth_token_I_got");
     },
}).done(function(data) {
console.log(data)
},"json");

}

I get 401 error whenever I try to make the call

Further Trace of the error

{
    "error": {
    "errors": [
         {
             "domain": "global",
             "reason": "required",
             "message": "Login Required",
             "locationType": "header",
             "location": "Authorization"
         }
    ],
    "code": 401,
    "message": "Login Required"
    }
}

Where am I mistake ? Thanks

1 Answers1

0

Since you are requesting the data form a different domain, jQuery is likely going to use JSONP and you can't set headers for JSONP requests (because they work via a script tag).

I don't know that particular API, but I would assume that there must be a JSONP compatible way to pass your auth-token (cookie, URL param, query param or something like that).

A related question and answer: Is it Possible to Make Cross-Domain Requests in Javascript AND Set Custom Headers?

You may also be able to use CORS and set headers that way.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979