如何使用Scribe通过POST向自定义API发出OAuth2授权请求

时间:2015-09-04 07:40:18

标签: java android scribe

你好开发人员......

我是oauth2的新手,我找到了适合我需要的Scribe Java Library ...但问题是我有自己的oauth2服务器,它通过POST接收请求,用户凭据通过PAYLOAD“application / x-www传递 - 形式进行了urlencoded“

您可在此处查看示例请求: SCREENSHOT

当我尝试使用文档

实现我自己的ApiClass时
  

https://github.com/fernandezpablo85/scribe-java/wiki/Custom-Apis

我发现客户端凭据已附加到网址

private static final String AUTHORIZE_URL = "http://jimbo.com/oauth/authorize?token=%s";

这意味着授权请求是通过GET :(

如何正确配置ApiClass以发出POST请求?

提前致谢:)

UPD:对于我正在使用的OAuth2服务器端

  

github.com/Filsh/yii2-oauth2-server

1 个答案:

答案 0 :(得分:2)

结果我从我的项目中删除了Scribe ...并实现了自己获取访问令牌的方法......

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://127.0.0.1/oauth2/token");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("grant_type", "password"));
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        nameValuePairs.add(new BasicNameValuePair("client_id", clientID));
        nameValuePairs.add(new BasicNameValuePair("client_secret", clientSecret));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        JSONObject json_auth = new JSONObject(EntityUtils.toString(response.getEntity()));
        String token = json_auth.getString("access_token");

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }