我目前正在尝试使用Venmo的Oauth API登录到基于Ionic Framework的应用程序。我正在尝试使用服务器端流程,这样我就可以拥有一个更长期的访问令牌。我能够接收代码并将其设置为requestToken变量。
但是,当我尝试使用我的客户端Id、客户端机密和请求令牌发送到"https://api.venmo.com/v1/oauth/access_token“时,我收到以下错误警报:" error : object Object”。
在检查我的控制台时,我发现错误是在我的post请求中出现了一个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的经验,将非常感谢您的帮助!
发布于 2015-08-11 10:35:08
我实际上能够用上面描述的方法解决这个问题。在我的原始代码中,我省略了用于将内容类型设置为URL编码的行(包含在Nic的示例中)。一旦我添加了这一行,请求就可以正常工作了。这行代码如下:
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';https://stackoverflow.com/questions/31575384
复制相似问题