Google Contacts API and Origin error

336 views Asked by At

I use Google Contacts API version 3.0 and when i want to call one of addresses for request data from contact list this error happened:

XMLHttpRequest cannot load https://www.google.com/m8/feeds/contacts/[email protected]/full. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://YYYY.ir' is therefore not allowed access. The response had HTTP status code 405.

And this is my code :

function PopupFunc() {
jQuery("input[title*='GetgoogleButt1']")
    .off()
    .on('click', function (event) {
        console.log('clicked');
        var lnk = "https://accounts.google.com/o/oauth2/v2/auth?" +
            //"scope=email profile&" +
            "scope=https://www.googleapis.com/auth/contacts.readonly&" +
            //"scope=https://www.googleapis.com/auth/user.phonenumbers.read&" +
            "state=/profile&" +
            "redirect_uri=http://XXXX/Mypagerdi.html&" +
            "response_type=token&" +
            "client_id=MUCLIENTID.apps.googleusercontent.com&" +
            "output=embed";

        var authTab = window.open(lnk, "Auth", "_blank, width=600, height=600");

        jQuery(window).off().on("message", function (e) {
            if ( e.originalEvent.origin == "http://XXXX/" ) {
                console.log("message is : ", e.originalEvent.data)
                var hashdic = e.originalEvent.data;
                var authrz = hashdic["token_type"] + ' ' + hashdic["access_token"];
                console.log(hashdic["expires_in"])
                console.log(authrz)
                if ( hashdic["#error"] == undefined ) {
                    jQuery.ajax({
                        url: "https://www.google.com/m8/feeds/contacts/[email protected]/full",
                        headers: {
                            'Authorization': authrz,
                            'Content-Type': 'application/json'
                        },
                        method: 'GET',
                        success: function (data) {
                            console.log('succes: ' + data);
                        }
                    });
                }
            }
        });
    });
}
PopupFunc();

I added Authorized JavaScript origins in credentials and this error still exist !

EDit :

this is missing part in ajax body dataType: 'jsonp' Thanks to @Mehmet

1

There are 1 answers

0
Mehmet On BEST ANSWER

Please try crossDomain: true and dataType: 'jsonp' in your ajax request.