]> www.average.org Git - WhereAmI.git/blob - Authorize.java
44a88a9b470d11943ed2c772d4311b91161a58e0
[WhereAmI.git] / Authorize.java
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
3
4 package org.average.whereami;
5
6 import java.io.IOException;
7
8 import org.average.whereami.ClientCredentials;
9 import org.average.whereami.CredentialStore;
10
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;
22
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;
28
29 public class Authorize extends Activity {
30
31     final String TAG = getClass().getName();
32
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";
38
39     @Override
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);
44     }
45
46     @Override
47     protected void onResume() {
48         super.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() {
58             @Override
59             public void onPageStarted(WebView view, String url, Bitmap bitmap) {
60                 Log.w(TAG, "onPageStarted : " + url);
61             }
62             @Override
63             public void onPageFinished(WebView view, String url) {
64                 if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
65                     try {
66                         if (url.indexOf("code=")!=-1) {
67                             String code = extractCodeFromUrl(url);
68                             AccessTokenResponse accessTokenResponse =
69                               new GoogleAuthorizationCodeGrant(
70                                 new NetHttpTransport(),
71                                 new JacksonFactory(),
72                                 ClientCredentials.CLIENT_ID,
73                                 ClientCredentials.CLIENT_SECRET,
74                                 code,
75                                 ClientCredentials.REDIRECT_URI).execute();
76                             CredentialStore credentialStore =
77                                                new CredentialStore(prefs);
78                             credentialStore.write(accessTokenResponse);
79                             finish();
80                             //view.setVisibility(View.INVISIBLE);
81                             //startActivity(new Intent(Authorize.this,
82                             //                         WhereAmI.class));
83                         } else if (url.indexOf("error=")!=-1) {
84                             new CredentialStore(prefs).clear();
85                             finish();
86                             //view.setVisibility(View.INVISIBLE);
87                             //startActivity(new Intent(Authorize.this,
88                             //                         WhereAmI.class));
89                         }
90                     } catch (IOException e) {
91                         e.printStackTrace();
92                     }
93                 }
94                 Log.w(TAG, "onPageFinished : " + url);
95             }
96
97             private String extractCodeFromUrl(String url) {
98                 return url.substring(
99                             ClientCredentials.REDIRECT_URI.length()+7,
100                             url.length());
101             }
102         });
103
104         webview.loadUrl(authorizationUrl);
105     }
106 }