]> www.average.org Git - WhereAmI.git/blob - src/org/average/whereami/Authorize.java
Implement OAuth2 authorization
[WhereAmI.git] / src / org / average / whereami / 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
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;
21
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;
27
28 public class Authorize extends Activity {
29
30     final String TAG = getClass().getName();
31
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";
37
38     @Override
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);
43     }
44
45     @Override
46     protected void onResume() {
47         super.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() {
57             @Override
58             public void onPageStarted(WebView view, String url, Bitmap bitmap) {
59                 Log.w(TAG, "onPageStarted : " + url);
60             }
61             @Override
62             public void onPageFinished(WebView view, String url) {
63                 if (url.startsWith(ClientCredentials.REDIRECT_URI)) {
64                     try {
65                         if (url.indexOf("code=")!=-1) {
66                             String code = extractCodeFromUrl(url);
67                             AccessTokenResponse accessTokenResponse =
68                               new GoogleAuthorizationCodeGrant(
69                                 new NetHttpTransport(),
70                                 new JacksonFactory(),
71                                 ClientCredentials.CLIENT_ID,
72                                 ClientCredentials.CLIENT_SECRET,
73                                 code,
74                                 ClientCredentials.REDIRECT_URI).execute();
75                             storeTokens(accessTokenResponse);
76                             finish();
77                             //view.setVisibility(View.INVISIBLE);
78                             //startActivity(new Intent(Authorize.this,
79                             //                         WhereAmI.class));
80                         } else if (url.indexOf("error=")!=-1) {
81                             clearTokens();
82                             finish();
83                             //view.setVisibility(View.INVISIBLE);
84                             //startActivity(new Intent(Authorize.this,
85                             //                         WhereAmI.class));
86                         }
87                     } catch (IOException e) {
88                         e.printStackTrace();
89                     }
90                 }
91                 Log.w(TAG, "onPageFinished : " + url);
92             }
93
94             private String extractCodeFromUrl(String url) {
95                 return url.substring(
96                             ClientCredentials.REDIRECT_URI.length()+7,
97                             url.length());
98             }
99
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);
111                 editor.commit();
112             }
113
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);
121                 editor.commit();
122             }
123         });
124
125         webview.loadUrl(authorizationUrl);
126     }
127 }