Last active
August 29, 2015 14:14
-
-
Save dhesson/c728dfc1c91648d9b41c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.TextView; | |
import com.google.api.client.auth.oauth2.BearerToken; | |
import com.google.api.client.auth.oauth2.ClientParametersAuthentication; | |
import com.google.api.client.auth.oauth2.Credential; | |
import com.google.api.client.extensions.android.http.AndroidHttp; | |
import com.google.api.client.http.GenericUrl; | |
import com.google.api.client.json.jackson.JacksonFactory; | |
import com.wuman.android.auth.AuthorizationFlow; | |
import com.wuman.android.auth.AuthorizationUIController; | |
import com.wuman.android.auth.DialogFragmentController; | |
import com.wuman.android.auth.OAuthManager; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
import butterknife.OnClick; | |
public class OAuthDemo extends Activity { | |
private static final String TAG = OAuthDemo.class.getSimpleName(); | |
private static final String CLIENT_ID = "<paste it>"; | |
private static final String CLIENT_SECRET = "<paste it>"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ButterKnife.inject(this); | |
// OAuth library @ https://github.com/wuman/android-oauth-client | |
} | |
@OnClick(R.id.auth) | |
public void doAuth() { | |
AuthorizationFlow.Builder builder = new AuthorizationFlow.Builder( | |
BearerToken.authorizationHeaderAccessMethod(), | |
AndroidHttp.newCompatibleTransport(), | |
new JacksonFactory(), | |
new GenericUrl("https://jawbone.com/auth/oauth2/token"), | |
new ClientParametersAuthentication(CLIENT_ID, CLIENT_SECRET), | |
CLIENT_ID, | |
"https://jawbone.com/auth/oauth2/auth"); | |
// more scopes @ https://jawbone.com/up/developer/authentication | |
builder.setScopes(Arrays.asList("extended_read", "friends_read")); | |
AuthorizationFlow flow = builder.build(); | |
AuthorizationUIController controller = | |
new DialogFragmentController(getFragmentManager()) { | |
@Override | |
public String getRedirectUri() throws IOException { | |
return "http://localhost/Callback"; | |
} | |
@Override | |
public boolean isJavascriptEnabledForWebView() { | |
return true; | |
} | |
}; | |
OAuthManager manager = new OAuthManager(flow, controller); | |
manager.authorizeExplicitly("testuser", new OAuthManager.OAuthCallback<Credential>() { | |
@Override | |
public void run(OAuthManager.OAuthFuture<Credential> future) { | |
try { | |
Credential cred = future.getResult(); // could be null... | |
String token = cred.getAccessToken(); | |
// do something with token (retrofit) | |
Log.d(TAG, "Token is: " + token); | |
} catch (IOException e) { | |
Log.e(TAG, "Rawr", e); | |
} | |
} | |
}, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment