JavaMachine
JavaMachine

Reputation: 631

How to make OAuth2 Authorization Request via POST to Custom API using Scribe

Hello fellow developers...

I am new to oauth2 and I found Scribe Java Library which suits my needs... but the problem is that I have my own oauth2 server which receives request via POST and user credentials are passed through PAYLOAD "application/x-www-form-urlencoded"

Here you can see sample request: SCREENSHOT

And when I try to implement my own ApiClass using documentation

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

and I noticed that client credentials are attached to url

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

which means authorization requests are made via GET :(

How to configure ApiClass properly to make POST request?

Thanks in advance :)

UPD: for the OAuth2 Server Side I am using

github.com/Filsh/yii2-oauth2-server

Upvotes: 4

Views: 8308

Answers (1)

JavaMachine
JavaMachine

Reputation: 631

As the result I deleted Scribe from my project... and implemented own method for getting access 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();
    }

Upvotes: 4

Related Questions