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;
9 import org.average.whereami.CredentialStore;
11 import android.app.Activity;
12 import android.os.Bundle;
13 import android.graphics.Bitmap;
14 import android.content.Intent;
15 import android.content.SharedPreferences;
16 import android.content.SharedPreferences.Editor;
17 import android.preference.PreferenceManager;
18 import android.util.Log;
19 import android.view.View;
20 import android.webkit.WebView;
21 import android.webkit.WebViewClient;
23 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
24 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
25 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
26 import com.google.api.client.http.javanet.NetHttpTransport;
27 import com.google.api.client.json.jackson.JacksonFactory;
29 public class Authorize extends Activity {
31 final String TAG = getClass().getName();
33 private SharedPreferences prefs;
34 private static final String ACCESS_TOKEN = "access_token";
35 private static final String EXPIRES_IN = "expires_in";
36 private static final String REFRESH_TOKEN = "refresh_token";
37 private static final String SCOPE = "scope";
40 public void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 Log.w(TAG, "Starting task to retrieve request token.");
43 prefs = PreferenceManager.getDefaultSharedPreferences(this);
47 protected void onResume() {
49 WebView webview = new WebView(this);
50 webview.getSettings().setJavaScriptEnabled(true);
51 webview.setVisibility(View.VISIBLE);
52 setContentView(webview);
53 String authorizationUrl = new GoogleAuthorizationRequestUrl(
54 ClientCredentials.CLIENT_ID,
55 ClientCredentials.REDIRECT_URI,
56 ClientCredentials.SCOPE).build();
57 webview.setWebViewClient(new WebViewClient() {
59 public void onPageStarted(WebView view, String url, Bitmap bitmap) {
60 Log.w(TAG, "onPageStarted : " + url);
63 public void onPageFinished(WebView view, String url) {
64 if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
66 if (url.indexOf("code=")!=-1) {
67 String code = extractCodeFromUrl(url);
68 AccessTokenResponse accessTokenResponse =
69 new GoogleAuthorizationCodeGrant(
70 new NetHttpTransport(),
72 ClientCredentials.CLIENT_ID,
73 ClientCredentials.CLIENT_SECRET,
75 ClientCredentials.REDIRECT_URI).execute();
76 CredentialStore credentialStore =
77 new CredentialStore(prefs);
78 credentialStore.write(accessTokenResponse);
80 //view.setVisibility(View.INVISIBLE);
81 //startActivity(new Intent(Authorize.this,
83 } else if (url.indexOf("error=")!=-1) {
84 new CredentialStore(prefs).clear();
86 //view.setVisibility(View.INVISIBLE);
87 //startActivity(new Intent(Authorize.this,
90 } catch (IOException e) {
94 Log.w(TAG, "onPageFinished : " + url);
97 private String extractCodeFromUrl(String url) {
99 ClientCredentials.REDIRECT_URI.length()+7,
104 webview.loadUrl(authorizationUrl);