Ajax post request question

edited July 2013 in Newbie Questions
I am trying to execute the following using enyo.ajax.
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
This is my code
var ajax = new enyo.Ajax({
                        url: 'https://accounts.google.com/o/oauth2/token',
                        method: 'POST',
                        contentType: 'application/x-www-form-urlencoded',
                        postBody: {
                                   code: code[1],
                                   client_id: myclient_id,
                                   client_secret: myclient_secret,
                                   redirect_uri: redirect_uri,
                                   grant_type: 'authorization_code'
                        },
           });
          ajax.response(this, "processResponse");
          ajax.error(this, "processError");
          ajax.go();
I have verified all information and they are correct, but ajax.error is called every time. I was not sure if I used postBody parameter correctly. Please help me out here. Thank you

Comments

  • What error code are you getting? Have you watched this transaction in the Network tab of Web Inspector?
  • edited July 2013
    I am getting error code 400
  • edited July 2013
    I have made this work using jquery. The code for that is as follows. I am just trying to use enyo.ajax. From the following code, I am able get the data I need. Here is the code that works for me.
    $.post('https://accounts.google.com/o/oauth2/token', {
                          code: code[1],
                          client_id: options.clientId,
                          client_secret: options.clientSecret,
                          redirect_uri: options.redirectUri,
                          grant_type: 'authorization_code'
                          }).done(function(data) {
                          
                              var theUrl = 'https://www.googleapis.com/drive/v2/files?access_token=' + data.access_token;
                              $.get(theUrl, function(data) {
                                    alert(data.kind);
                              });
                              
                          }).fail(function(response) {
                              alert("Failed to connect")
                          });
  • Hey unwiredben ... I figured out the problem ... thanks anyways for your help. The issue is with postBody. I changed it to this postBody: 'code=' + code[1] + '&client_id=' + client_id .... and so on.
  • I suspect the issue might be that Enyo defaults to using cache buster methods which add a random numeric string to the URL. Instead of building your own postBody, try setting the "cacheBust: false" property for the enyo.Ajax object.
Sign In or Register to comment.