1

I am currently trying to login to my app that is built on Ionic Framework using Venmo's Oauth API. I am attempting to use the Server Side Flow so that I can have a longer term access token. I am able to receive a code and set it to a requestToken variable.

However, when I attempt to post to "https://api.venmo.com/v1/oauth/access_token" with my Client Id, Client Secret, and Request Token, I get the following error alert: "ERROR: [object Object]".

In checking my console, I see that the error is a 400 Bad Request error coming on my post request, although it does appear that I have a valid request token. The error message is as follows: "Failed to load resource: the server responded with a status of 400 (Bad Request)".

Below is the code of the login function I am using to login via Venmo's Oauth API:

//VENMO SERVER SIDE API FUNCTION
var requestToken = "";
var accessToken = "";
var clientId = "CLIENT_ID_HERE";
var clientSecret = "CLIENT_SECRET_HERE";

$scope.login = function() {
  var ref = window.open('https://api.venmo.com/v1/oauth/authorize?client_id=' + clientId + '&scope=make_payments%20access_profile%20access_friends&response_type=code');
  ref.addEventListener('loadstart', function(event) {
    if ((event.url).startsWith("http://localhost/callback")) {
      requestToken = (event.url).split("?code=")[1];
      console.log("Request Token = " + requestToken);
      $http({
          method: "post",
          url: "https://api.venmo.com/v1/oauth/access_token",
          data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + requestToken
        })
        .success(function(data) {
          accessToken = data.access_token;
          $location.path("/make-bet");
        })
        .error(function(data, status) {
          alert("ERROR: " + data);
        });
      ref.close();
    }
  });
}

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function(str) {
    return this.indexOf(str) == 0;
  };
}

This function is from this helpful walkthrough article by Nic Raboy (https://blog.nraboy.com/2014/07/using-oauth-2-0-service-ionicframework/). I think that the problem may be in how I am presenting the data array, so if anyone has any experience in successfully implementing a Venmo API in Ionic, your help would be much appreciated!

dglenn
  • 11
  • 3

1 Answers1

0

I was actually able to solve this issue with the method described above. In my original code, I omitted the line used to set the content type to URL encoded (which was included in Nic's example). Once I added this line, the request functioned as expected. The line was as follows:

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
dglenn
  • 11
  • 3