A fully functional example application demonstrating the use of the Coinbase Android SDK
Building the app is only supported in Android Studio. Steps to build:
git clone [email protected]:coinbase/coinbase-android-sdk-example.git
- Open Android Studio, and close any open project
- Click 'Import project...'
- Open the
coinbase-android-sdk-example
directory downloaded in step 1 - That's it! You should be able to build and run the app from inside Android Studio.
In app/build.gradle:
compile ('com.coinbase.android:coinbase-android-sdk:1.0.1)
In app/src/main/java/com/coinbase/android/sdk/example/MainActivity.java:
import com.coinbase.android.sdk.OAuth;
// ...
OAuth.beginAuthorization(this, CLIENT_ID, "user", REDIRECT_URI, null);
In app/src/main/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:scheme="coinbase-android-example" android:pathPrefix="coinbase-oauth" />
</intent-filter>
In app/src/main/java/com/coinbase/android/sdk/example/MainActivity.java:
// Completing the authorization must be done in an async task since it requires network communication...
public class CompleteAuthorizationTask extends RoboAsyncTask<OAuthTokensResponse> {
private Intent mIntent;
public CompleteAuthorizationTask(Intent intent) {
super(MainActivity.this);
mIntent = intent;
}
@Override
public OAuthTokensResponse call() throws Exception {
return OAuth.completeAuthorization(MainActivity.this, CLIENT_ID, CLIENT_SECRET, mIntent.getData());
}
@Override
public void onSuccess(OAuthTokensResponse tokens) {
// Success, now do something with the tokens
new DisplayEmailTask(tokens).execute();
}
@Override
public void onException(Exception ex) {
mTextView.setText("There was an error fetching access tokens using the auth code: " + ex.getMessage());
}
}
// In the Activity we set up to listen to our redirect URI
@Override
protected void onNewIntent(final Intent intent) {
if (intent != null && intent.getAction() != null && intent.getAction().equals("android.intent.action.VIEW")) {
new CompleteAuthorizationTask(intent).execute();
}
}