各位开发者大家好...
我是 oauth2 的新手,我找到了适合我需要的 Scribe Java 库...但问题是我有自己的 oauth2 服务器,它通过 POST 接收请求,用户凭据通过 PAYLOAD "application/x-www - 形式 urlencoded"
在这里你可以看到示例请求: SCREENSHOT
当我尝试使用文档实现我自己的 ApiClass 时
https://github.com/fernandezpablo85/scribe-java/wiki/Custom-Apis
我注意到客户端凭据附加到 url
private static final String AUTHORIZE_URL = "http://jimbo.com/oauth/authorize?token=%s";
这意味着授权请求是通过 GET 进行的:(
如何正确配置 ApiClass 以发出 POST 请求?
提前致谢:)
UPD:对于我正在使用的 OAuth2 服务器端
github.com/Filsh/yii2-oauth2-server
最佳答案
结果我从我的项目中删除了 Scribe...并实现了自己的获取访问 token 的方法...
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();
}
关于java - 如何使用 Scribe 通过 POST 向自定义 API 发出 OAuth2 授权请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32392674/