1 // http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/
2 // http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10
4 package org.average.whereami;
6 import java.io.IOException;
8 import org.average.whereami.ClientCredentials;
10 import android.app.Activity;
11 import android.os.Bundle;
12 import android.graphics.Bitmap;
13 import android.content.Intent;
14 import android.content.SharedPreferences;
15 import android.content.SharedPreferences.Editor;
16 import android.preference.PreferenceManager;
17 import android.util.Log;
18 import android.view.View;
19 import android.webkit.WebView;
20 import android.webkit.WebViewClient;
22 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
23 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
24 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
25 import com.google.api.client.http.javanet.NetHttpTransport;
26 import com.google.api.client.json.jackson.JacksonFactory;
28 public class Authorize extends Activity {
30 final String TAG = getClass().getName();
32 private SharedPreferences prefs;
33 private static final String ACCESS_TOKEN = "access_token";
34 private static final String EXPIRES_IN = "expires_in";
35 private static final String REFRESH_TOKEN = "refresh_token";
36 private static final String SCOPE = "scope";
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 Log.w(TAG, "Starting task to retrieve request token.");
42 prefs = PreferenceManager.getDefaultSharedPreferences(this);
46 protected void onResume() {
48 WebView webview = new WebView(this);
49 webview.getSettings().setJavaScriptEnabled(true);
50 webview.setVisibility(View.VISIBLE);
51 setContentView(webview);
52 String authorizationUrl = new GoogleAuthorizationRequestUrl(
53 ClientCredentials.CLIENT_ID,
54 ClientCredentials.REDIRECT_URI,
55 ClientCredentials.SCOPE).build();
56 webview.setWebViewClient(new WebViewClient() {
58 public void onPageStarted(WebView view, String url, Bitmap bitmap) {
59 Log.w(TAG, "onPageStarted : " + url);
62 public void onPageFinished(WebView view, String url) {
63 if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
65 if (url.indexOf("code=")!=-1) {
66 String code = extractCodeFromUrl(url);
67 AccessTokenResponse accessTokenResponse =
68 new GoogleAuthorizationCodeGrant(
69 new NetHttpTransport(),
71 ClientCredentials.CLIENT_ID,
72 ClientCredentials.CLIENT_SECRET,
74 ClientCredentials.REDIRECT_URI).execute();
75 storeTokens(accessTokenResponse);
77 //view.setVisibility(View.INVISIBLE);
78 //startActivity(new Intent(Authorize.this,
80 } else if (url.indexOf("error=")!=-1) {
83 //view.setVisibility(View.INVISIBLE);
84 //startActivity(new Intent(Authorize.this,
87 } catch (IOException e) {
91 Log.w(TAG, "onPageFinished : " + url);
94 private String extractCodeFromUrl(String url) {
96 ClientCredentials.REDIRECT_URI.length()+7,
100 private void storeTokens(AccessTokenResponse accessTokenResponse) {
101 Log.w(TAG, "Storing tokens: " + accessTokenResponse);
102 Editor editor = prefs.edit();
103 editor.putString(ACCESS_TOKEN,
104 accessTokenResponse.accessToken);
105 editor.putLong( EXPIRES_IN,
106 accessTokenResponse.expiresIn);
107 editor.putString(REFRESH_TOKEN,
108 accessTokenResponse.refreshToken);
109 editor.putString(SCOPE,
110 accessTokenResponse.scope);
114 private void clearTokens() {
115 Log.w(TAG, "Clear tokens");
116 Editor editor = prefs.edit();
117 editor.remove(ACCESS_TOKEN);
118 editor.remove(EXPIRES_IN);
119 editor.remove(REFRESH_TOKEN);
120 editor.remove(SCOPE);
125 webview.loadUrl(authorizationUrl);