Giter VIP home page Giter VIP logo

coinbase-android-sdk-example's Introduction

coinbase-android-sdk-example

A fully functional example application demonstrating the use of the Coinbase Android SDK

Building

Building the app is only supported in Android Studio. Steps to build:

  1. git clone [email protected]:coinbase/coinbase-android-sdk-example.git
  2. Open Android Studio, and close any open project
  3. Click 'Import project...'
  4. Open the coinbase-android-sdk-example directory downloaded in step 1
  5. That's it! You should be able to build and run the app from inside Android Studio.

Code Highlights

Importing the SDK

In app/build.gradle:

compile ('com.coinbase.android:coinbase-android-sdk:1.0.1)

Redirecting the user to Coinbase for authorization

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);

Listening for redirect uri to re-open example application

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>

Handling redirect back to example application and completing authorization:

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();
  }
}

coinbase-android-sdk-example's People

Contributors

aianus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.