Search code examples
androidrestoauth-2.0bitbucket-api

Redirect_URI for Android REST client


I am building a REST client on android to communicate with the Bitbucket API. I am having problem with OAUTH2.0.

  • From the docs provided by Bitbucket, you have to create a "consumer" in order to obtain the ClientID(Key) and Secret to use for your android app
  • And in the consumer creation form, there's the "Callback URL" (which I understand as the webpage or location that Bitbucket will take user to after they have finished granted permission for the app (i.e. logging in)
  • In my android app, I want the android device to go back to my app after user has granted permission (like how you login to a website with google account, after filling in your credentials on the permission page of google, you are taken back to the original page you were on)

The tutorial I was following implements this by

  • Adding this in the activity in which the login takes place in manifest.xml

<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="redirecturi" android:scheme="your" /> </intent-filter>

  • Send this to Bitbucket

    private final String redirectUri = "your://redirecturi";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button loginButton = (Button) findViewById(R.id.loginbutton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        Intent.ACTION_VIEW,
                        //Uri.parse("" + "/login" + "?client_id=" + clientId + "&redirect_uri=" + redirectUri));
                        Uri.parse("https://bitbucket.org/site/oauth2/authorize"  + "?client_id=" + clientId + "&redirect_uri=" + redirectUri));
                startActivity(intent);
            }
        });
    }
    

My question is: - How do I config the redirect URI or callback URL on Bitbucket consumer object and in my app's request to make sure the android device goes back to my app and so my app can catch responses from Bitbucket (and this response should have the access_token which I'll need).


Solution

  • An easy way to manage this is using a WebView rather than opeing your OAuth link through an intent. You can actually check the url whenever you are redirected in a WebView. To do so you have to override

    public boolean shouldOverrideUrlLoading(WebView view, String url)

    whenever you are redirected to another link ( or your callback uri in this case ) you can check or compare the url obtained and proceed accordingly.