]> www.average.org Git - WhereAmI.git/blobdiff - src/org/average/whereami/Authorize.java
Implement OAuth2 authorization
[WhereAmI.git] / src / org / average / whereami / Authorize.java
diff --git a/src/org/average/whereami/Authorize.java b/src/org/average/whereami/Authorize.java
new file mode 100644 (file)
index 0000000..1bf5fad
--- /dev/null
@@ -0,0 +1,127 @@
+// http://blog.doityourselfandroid.com/2011/08/06/oauth-2-0-flow-android/
+// http://code.google.com/p/google-api-java-client/wiki/OAuth2Draft10
+
+package org.average.whereami;
+
+import java.io.IOException;
+
+import org.average.whereami.ClientCredentials;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.graphics.Bitmap;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.preference.PreferenceManager;
+import android.util.Log;
+import android.view.View;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
+import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
+import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.jackson.JacksonFactory;
+
+public class Authorize extends Activity {
+
+    final String TAG = getClass().getName();
+
+    private SharedPreferences prefs;
+    private static final String ACCESS_TOKEN = "access_token";
+    private static final String EXPIRES_IN = "expires_in";
+    private static final String REFRESH_TOKEN = "refresh_token";
+    private static final String SCOPE = "scope";
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Log.w(TAG, "Starting task to retrieve request token.");
+        prefs = PreferenceManager.getDefaultSharedPreferences(this);
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        WebView webview = new WebView(this);
+        webview.getSettings().setJavaScriptEnabled(true);
+        webview.setVisibility(View.VISIBLE);
+        setContentView(webview);
+        String authorizationUrl = new GoogleAuthorizationRequestUrl(
+                                        ClientCredentials.CLIENT_ID,
+                                        ClientCredentials.REDIRECT_URI,
+                                        ClientCredentials.SCOPE).build();
+        webview.setWebViewClient(new WebViewClient() {
+            @Override
+            public void onPageStarted(WebView view, String url, Bitmap bitmap) {
+                Log.w(TAG, "onPageStarted : " + url);
+            }
+            @Override
+            public void onPageFinished(WebView view, String url) {
+                if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
+                    try {
+                        if (url.indexOf("code=")!=-1) {
+                            String code = extractCodeFromUrl(url);
+                            AccessTokenResponse accessTokenResponse =
+                              new GoogleAuthorizationCodeGrant(
+                                new NetHttpTransport(),
+                                new JacksonFactory(),
+                                ClientCredentials.CLIENT_ID,
+                                ClientCredentials.CLIENT_SECRET,
+                                code,
+                                ClientCredentials.REDIRECT_URI).execute();
+                            storeTokens(accessTokenResponse);
+                            finish();
+                            //view.setVisibility(View.INVISIBLE);
+                            //startActivity(new Intent(Authorize.this,
+                            //                         WhereAmI.class));
+                        } else if (url.indexOf("error=")!=-1) {
+                            clearTokens();
+                            finish();
+                            //view.setVisibility(View.INVISIBLE);
+                            //startActivity(new Intent(Authorize.this,
+                            //                         WhereAmI.class));
+                        }
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+                Log.w(TAG, "onPageFinished : " + url);
+            }
+
+            private String extractCodeFromUrl(String url) {
+                return url.substring(
+                            ClientCredentials.REDIRECT_URI.length()+7,
+                            url.length());
+            }
+
+            private void storeTokens(AccessTokenResponse accessTokenResponse) {
+                Log.w(TAG, "Storing tokens: " + accessTokenResponse);
+                Editor editor = prefs.edit();
+                editor.putString(ACCESS_TOKEN,
+                                 accessTokenResponse.accessToken);
+                editor.putLong(  EXPIRES_IN,
+                                 accessTokenResponse.expiresIn);
+                editor.putString(REFRESH_TOKEN,
+                                 accessTokenResponse.refreshToken);
+                editor.putString(SCOPE,
+                                 accessTokenResponse.scope);
+                editor.commit();
+            }
+
+            private void clearTokens() {
+                Log.w(TAG, "Clear tokens");
+                Editor editor = prefs.edit();
+                editor.remove(ACCESS_TOKEN);
+                editor.remove(EXPIRES_IN);
+                editor.remove(REFRESH_TOKEN);
+                editor.remove(SCOPE);
+                editor.commit();
+            }
+        });
+
+        webview.loadUrl(authorizationUrl);
+    }
+}