Callback URL example with Instagram OAuth
suggest changeOne of the use cases of callback URLs is OAuth. Let us do this with an Instagram Login: If the user enters their credentials and clicks the Login button, Instagram will validate the credentials and return an access_token. We need that access_token in our app.
For the app to hear those links, add a callback URL to an activity. An <intent-filter> on that activity reacts to the URL. If the callback URL is appSchema://appName.com, put this filter inside the activity in AndroidManifest.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="appName.com" android:scheme="appSchema" />
</intent-filter>
CATEGORY_DEFAULTis required for an implicit intent to match.CATEGORY_BROWSABLElets a browser start the activity from a link.<data>sets the scheme and host. The word is "scheme", not "schema".- Together, these lines open the activity when that callback URL is opened.
Now, in order to get the contents of the URL in your Activity, you need to override the onResume() method as follows:
@Override
public void onResume() {
super.onResume();
// The string resource is "appSchema://appName.com".
String CALLBACK_URL = getResources().getString(R.string.insta_callback);
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String accessToken = uri.getQueryParameter("access_token");
}
// Perform other operations here.
}
getQueryParameter reads ?access_token=. Instagram's implicit flow puts the token in the fragment (#access_token=), which this call does not see. That access_token is what Instagram's API endpoints expect.