1

我目前正在尝试使用 Venmo 的 Oauth API 登录到基于 Ionic Framework 构建的应用程序。我正在尝试使用服务器端流程,以便获得更长期的访问令牌。我能够接收代码并将其设置为 requestToken 变量。

但是,当我尝试使用我的客户端 ID、客户端密码和请求令牌发布到“ https://api.venmo.com/v1/oauth/access_token ”时,我收到以下错误警报:“错误:[object Object ]”。

在检查我的控制台时,我看到错误是我的发布请求出现 400 Bad Request 错误,尽管看起来我确实有一个有效的请求令牌。错误信息如下:“加载资源失败:服务器响应状态为 400(错误请求)”。

下面是我用来通过 Venmo 的 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;
  };
}

此功能来自 Nic Raboy 的这篇有用的演练文章 ( https://blog.nraboy.com/2014/07/using-oauth-2-0-service-ionicframework/ )。我认为问题可能在于我如何呈现数据数组,所以如果有人在 Ionic 中成功实现 Venmo API 方面有任何经验,我们将不胜感激!

4

1 回答 1

0

我实际上能够用上述方法解决这个问题。在我的原始代码中,我省略了用于将内容类型设置为 URL 编码的行(包含在 Nic 的示例中)。一旦我添加了这一行,请求就会按预期运行。该行如下:

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
于 2015-08-11T02:35:08.207 回答