【问题标题】:Oauth2 Get access_token from UBEROauth2 从 UBER 获取 access_token
【发布时间】: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。

【问题讨论】:

    标签: java post oauth uber-api


    【解决方案1】:

    您是否尝试过使用库来处理 OAuth ? 文档说“我们建议您使用预构建的库来执行授权和令牌交换”https://developer.uber.com/v1/auth/。 我也面临一些问题,但至少我没有得到 400 的回报:-)。

    马特

    【讨论】:

    • 是的,我尝试使用谷歌库。最后我解决了它,请参阅编辑问题。
    猜你喜欢
    • 2018-07-04
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多