【发布时间】:2015-08-10 23:58:38
【问题描述】:
我正在尝试将授权代码转换为访问令牌,但我收到“错误请求” (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", 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();
UBER 相关docs。
我正在寻找两天的解决方案。另外,我检查了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);
一件重要的事情: 他们要求您插入您在上一个请求中插入的相同 redirect_url。
【问题讨论】: