android, oauth 1.0a, scribe = exception

410 views Asked by At

I am getting these error when retrieving ucoz.api.ru (oauth 1.0a) token using scribe library oauth (4.2.0) on android :

Caused by: com.github.scribejava.core.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this:

'{"oauth_token":"NAzoveaGm5XIlBvLcLRxUvamEK8P2.BAlQZ.M.aV","oauth_token_secret":"SJsqC0IfFAKS3BkdauQ3bY4ha01PDHTlFIy7GSro","oauth_callback_confirmed":"true"}'

at com.github.scribejava.core.extractors.AbstractOAuth1TokenExtractor.extract(AbstractOAuth1TokenExtractor.java:42) at com.github.scribejava.core.extractors.AbstractOAuth1TokenExtractor.extract(AbstractOAuth1TokenExtractor.java:32) at com.github.scribejava.core.extractors.AbstractOAuth1TokenExtractor.extract(AbstractOAuth1TokenExtractor.java:19) at com.github.scribejava.core.oauth.OAuth10aService.getRequestToken(OAuth10aService.java:49) at com.vasyaevstropov.oauth10test.MainActivity.request(MainActivity.java:96) at com.vasyaevstropov.oauth10test.MainActivity$1$1.doInBackground(MainActivity.java:61) at com.vasyaevstropov.oauth10test.MainActivity$1$1.doInBackground(MainActivity.java:53) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  at java.lang.Thread.run(Thread.java:841)

My source code:

  final OAuth10aService service = new ServiceBuilder(CONSUMER_KEY)
                .apiSecret(CONSUMER_SECRET)
                .debug()
                .build(UcozApi.instance());

        final Scanner in = new Scanner(System.in);
        // Obtain the Request Token
        final OAuth1RequestToken requestToken = service.getRequestToken(); // <<--- Error is in this place
        System.out.println(service.getAuthorizationUrl(requestToken));
        final String oauthVerifier = in.nextLine();
        // Trade the Request Token and Verfier for the Access Token
        OAuth1AccessToken accessToken = null;
        try {
            accessToken = service.getAccessToken(requestToken, oauthVerifier);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

Ucozapi module:

public class UcozApi extends com.github.scribejava.core.builder.api.DefaultApi10a {

    private static final String AUTHORIZE_URL = "http://uapi.ucoz.com/accounts/oauthauthorizetoken=%s";
    private static final String REQUEST_TOKEN_RESOURCE = "http://uapi.ucoz.com/accounts/oauthgetrequesttoken";
    private static final String ACCESS_TOKEN_RESOURCE = "http://uapi.ucoz.com/accounts/oauthgetaccesstoken";

    protected UcozApi() {
    }

    private static final UcozApi INSTANCE = new UcozApi();


    public static UcozApi instance() {
        return INSTANCE;
    }

    @Override
    public String getAccessTokenEndpoint() {
        return ACCESS_TOKEN_RESOURCE;
    }

    @Override
    public String getRequestTokenEndpoint() {
        return REQUEST_TOKEN_RESOURCE;
    }

    @Override
    public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
        return String.format(AUTHORIZE_URL, requestToken.getToken());
    }
}

Can somebody help me?

1

There are 1 answers

1
Evstropov V On BEST ANSWER

I answer my question. This code will work good with scribe-java library:

MainActivity:

import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.oauth.OAuth10aService;
import com.vasyaevstropov.oauthtest.ucoz.UcozApi;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {
    Button button;
    public WebView webView;
    String verifier;
    OAuth1RequestToken requestToken = null;
    OAuth10aService service;
    OAuth1AccessToken accessToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        webView.clearCache(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        service = new ServiceBuilder("murka1")
                .apiSecret("DqUQJzeCPmwD9CRqbHo6sGBzKCb5U4")
                .debug()
                .build(UcozApi.instance());

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask<Void, Void, String>() {
                    protected String doInBackground(Void... params) {
                        String PROTECTED_RESOURCE_URL = "http://artmurka.com/uapi/shop/request?page=categories";
                        try {
                            requestToken = service.getRequestToken();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                        String url = service.getAuthorizationUrl(requestToken);
                        return url;
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        loadURL(result);
                    }
                }.execute();
            }
        });
    }

    public void loadURL(final String url) {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                if (url.contains("oauth_verifier")) {
                    webView.setVisibility(webView.GONE);
                    Log.d("Log.d", url);
                    verifier = uri.getQueryParameter("oauth_verifier");
                    Toast.makeText(getApplicationContext(), verifier, Toast.LENGTH_SHORT).show();
                    getAccessToken();
                }
                return false;
            }
        });
        webView.loadUrl(url);
    }

    private void getAccessToken() {
        new AsyncTask<Void, Void, OAuth1AccessToken>() {
            protected OAuth1AccessToken doInBackground(Void... params) {
                try {
                    accessToken = service.getAccessToken(requestToken, verifier);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                return accessToken;
            }

            @Override
            protected void onPostExecute(OAuth1AccessToken result) {
                Toast.makeText(getApplicationContext(), "Token = " + result.getToken() + "Secret = " + result.getTokenSecret(), Toast.LENGTH_LONG).show();
            }
        }.execute();
    }
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

UcozApi

public class UcozApi extends DefaultApi10a {
    private static final String AUTHORIZE_URL = "http://uapi.ucoz.com/accounts/oauthauthorizetoken?oauth_token=%s";
    protected UcozApi() {
    }
    private static class InstanceHolder {
        private static final UcozApi INSTANCE = new UcozApi();
    }

    public static UcozApi instance() {
        return InstanceHolder.INSTANCE; }

    @Override
    public String getAccessTokenEndpoint(){
        return "http://uapi.ucoz.com/accounts/oauthgetaccesstoken"; }

    @Override
    public String getRequestTokenEndpoint() {
        return "http://uapi.ucoz.com/accounts/oauthgetrequesttoken"; }

    @Override
    public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
        return String.format(AUTHORIZE_URL, requestToken.getToken()); }

    @Override
    public TokenExtractor<OAuth1AccessToken> getAccessTokenExtractor() {
           return OAuth1AccessUcozTokenExtractor.instance();
    }

    @Override
    public TokenExtractor<OAuth1RequestToken> getRequestTokenExtractor() {
        return OAuth1RequestUcozTokenExtractor.instance();
    }
}

OAuth1RequestUcozTokenExtractor

import com.github.scribejava.core.model.OAuth1RequestToken;

public class OAuth1RequestUcozTokenExtractor extends AbstractOauth1UcozTokenExtractor<OAuth1RequestToken> {

    protected OAuth1RequestUcozTokenExtractor() {
    }

    @Override
    protected OAuth1RequestToken createToken(String token, String secret, String response) {
        return new OAuth1RequestToken(token, secret, response);
    }

    private static class InstanceHolder {

        private static final OAuth1RequestUcozTokenExtractor INSTANCE = new OAuth1RequestUcozTokenExtractor();
    }

    public static OAuth1RequestUcozTokenExtractor instance() {
        return InstanceHolder.INSTANCE;
    }
}

OAuth1AccessUcozTokenExtractor

public class OAuth1AccessUcozTokenExtractor extends AbstractOauth1UcozTokenExtractor<OAuth1AccessToken> {

    protected OAuth1AccessUcozTokenExtractor() {
    }

    @Override
    protected OAuth1AccessToken createToken(String token, String secret, String response) {
        return new OAuth1AccessToken(token, secret, response);
    }

    private static class InstanceHolder {

        private static final OAuth1AccessUcozTokenExtractor INSTANCE = new OAuth1AccessUcozTokenExtractor();
    }

    public static OAuth1AccessUcozTokenExtractor instance() {
        return InstanceHolder.INSTANCE;
    }
}

AbstractOauth1UcozTokenExtractor

public abstract class AbstractOauth1UcozTokenExtractor<T extends OAuth1Token> implements TokenExtractor<T> {

    private Pattern OAUTH_TOKEN_PATTERN = Pattern.compile("\"oauth_token\"\\s*:\\s*\"(\\S*?)\"");
    private Pattern OAUTH_TOKEN_SECRET_PATTERN = Pattern.compile("\"oauth_token_secret\"\\s*:\\s*\"(\\S*?)\"");

    @Override
    public T extract(Response response) throws IOException {
        final String body = response.getBody();
        Preconditions.checkEmptyString(body,
                "Response body is incorrect. " + "Can't extract a token from an empty string");
        final String token = extract(body, OAUTH_TOKEN_PATTERN);
        final String secret = extract(body, OAUTH_TOKEN_SECRET_PATTERN);
        return createToken(token, secret, body);
    }

    private String extract(String response, Pattern p) {
        final Matcher matcher = p.matcher(response);
        if (matcher.find() && matcher.groupCount() >= 1) {
            return OAuthEncoder.decode(matcher.group(1));
        } else {
            throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '"
                    + response + "'", null);
        }
    }

    protected abstract T createToken(String token, String secret, String response);
}