2015-09-04 55 views
3

你好的开发伙伴通过POST做出的OAuth2授权请求自定义API ...如何使用划线

我是新来的oauth2,我发现它适合我的需要抄写Java库......但问题是,我有它通过POST接收请求和用户凭据通过有效载荷“应用程序/ x-WWW的形式,进行了urlencoded”

在这里你可以看到样品的请求度过了自己的oauth2服务器: 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

+0

Try usi在Android中的RestTemplate用于发布帖子,获取或交换类型的请求。 –

+0

如果你愿意,我总是可以放一些样品,但网上已有大量样品。 –

+0

@ WeareBorg谢谢你的回答:)我正在考虑为OAuth2实施我自己的图书馆...任何帮助赞赏^ _^ – JavaMachine

回答

1

至于结果我从我的项目中删除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(); 
    } 
相关问题