Java Oauth2从UBER获取访问令牌
我试图将授权码转换为访问令牌,但收到“错误请求” (400)和空响应体 这是我的密码:Java Oauth2从UBER获取访问令牌,java,post,oauth,uber-api,Java,Post,Oauth,Uber Api,我试图将授权码转换为访问令牌,但收到“错误请求” (400)和空响应体 这是我的密码: Scanner in = new Scanner(System.in); HttpClient client = new HttpClient(); PostMethod post = new PostMethod("https://login.uber.com/oauth/token"); post.setRequestHeader("Content-Type", URL
Scanner in = new Scanner(System.in);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("https://login.uber.com/oauth/token");
post.setRequestHeader("Content-Type", URLEncoder.encode("application/x-www-form-urlencoded", "UTF-8"));
post.setParameter("grant_type",URLEncoder.encode("authorization_code", "UTF-8"));
post.setParameter("client_id",URLEncoder.encode("mq_LxaL0P-Kn8Wq3TmuhztFkOhISxReq", "UTF-8"));
post.setParameter("client_secret",URLEncoder.encode("MY_SECRET", "UTF-8"));
post.setParameter("redirect_uri",URLEncoder.encode("https://jfpmrvbais.localtunnel.me/TaxyNow/webresources/generic", "UTF-8"));
post.setParameter("code", URLEncoder.encode("S9UOTmXLGp20GF6y7TFf9pw5Pkekrl", "UTF-8"));
client.executeMethod(post);
String responseBody = post.getResponseBodyAsString();
String statusCode= post.getStatusText();
优步相关
我要找两天的解决办法。此外,我检查了stackoverflow中所有类似的问题,没有发现任何问题
非常感谢
编辑:
问题解决后,工作代码如下:
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("https://login.uber.com/oauth/token");
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
post.setRequestHeader("Accept", "application/json");
post.setParameter("client_secret",MY_SECRET);
post.setParameter("client_id",clientId);
post.setParameter("grant_type", "authorization_code");
post.setParameter("redirect_uri", REDIRECT_URL);
post.setParameter("code", code);
有一点很重要:
他们要求您插入与上次请求中插入的重定向url相同的url。您是否尝试过使用库来处理OAuth? doc说“我们建议您使用预构建的库来执行授权和令牌交换”。 我也面临着一些问题,但至少我没有得到400回:-)
马特是的,我试过谷歌图书馆。。最后我解决了它,请看编辑问题。