Atul O Holic
Atul O Holic

Reputation: 6792

Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth)

I am using signpost for OAuth to access data from a Magento server. I have read various tutorials on the same and I reach to the point where we open a browser so that user can enter his credentials. However, as per my requirement I have to automate this part.

Hence, user should not get the browser page. I have this set-up done on the server side (Magento), where I hit the URL and get returned to the call back page. I would like to do the same through my program in Android.

I have tried the below,

consumer = new CommonsHttpOAuthConsumer(KEY, Secret);
provider = new CommonsHttpOAuthProvider(OAUTH_INIT_URL,ACCESS_TOKEN_URL, AUTHORIZE_URL);                
try {
    provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
    Log.d("Tokens" , consumer.getToken() + " -- " + consumer.getTokenSecret());
    }

and I get the request tokens. But I dont know how to bypass the next step. I tried directly accessing the AccessToken (stupid of me) provider.retrieveAccessToken(consumer, "myCallback://callback"); but no luck, it ends up in

oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server   replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.

I would really appreciate any help, as I am stuck on this from last 3 days. Please tell me if I need to provide any more data.

Upvotes: 2

Views: 464

Answers (1)

Nirav Bhandari
Nirav Bhandari

Reputation: 4610

            OAuthConsumer consumer = new DefaultOAuthConsumer(myconsumerkey,
               mysecretkey);

        HttpURLConnection request;
        URL  baseUrl = new URL(myurl);
        request = (HttpURLConnection) baseUrl.openConnection();
        consumer.sign(request);

        InputStream in = new BufferedInputStream(
                    request.getInputStream());
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
            StringBuilder out = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                out.append(line);
            }
         String serverResponse = out.toString();

Upvotes: 1

Related Questions