SHOW:
|
|
- or go back to the newest paste.
| 1 | /* | |
| 2 | * Copyright 2011 Uwe Trottmann | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | * | |
| 16 | */ | |
| 17 | ||
| 18 | package net.asturdroid.datamovie.TMDBApi; | |
| 19 | ||
| 20 | import net.asturdroid.datamovie.Constants; | |
| 21 | import net.asturdroid.datamovie.R; | |
| 22 | import net.asturdroid.datamovie.Utils; | |
| 23 | import net.asturdroid.datamovie.ui.BaseActivity; | |
| 24 | import android.content.Context; | |
| 25 | import android.content.SharedPreferences; | |
| 26 | import android.net.Uri; | |
| 27 | import android.os.AsyncTask; | |
| 28 | import android.os.Bundle; | |
| 29 | import android.util.Log; | |
| 30 | import android.webkit.WebChromeClient; | |
| 31 | import android.webkit.WebView; | |
| 32 | import android.webkit.WebViewClient; | |
| 33 | import android.widget.Toast; | |
| 34 | ||
| 35 | import com.actionbarsherlock.app.ActionBar; | |
| 36 | import com.actionbarsherlock.app.SherlockFragmentActivity; | |
| 37 | import com.actionbarsherlock.view.Window; | |
| 38 | ||
| 39 | import oauth.signpost.OAuth; | |
| 40 | import oauth.signpost.OAuthConsumer; | |
| 41 | import oauth.signpost.OAuthProvider; | |
| 42 | import oauth.signpost.basic.DefaultOAuthConsumer; | |
| 43 | import oauth.signpost.basic.DefaultOAuthProvider; | |
| 44 | import oauth.signpost.exception.OAuthCommunicationException; | |
| 45 | import oauth.signpost.exception.OAuthExpectationFailedException; | |
| 46 | import oauth.signpost.exception.OAuthMessageSignerException; | |
| 47 | import oauth.signpost.exception.OAuthNotAuthorizedException; | |
| 48 | ||
| 49 | /** | |
| 50 | * Executes the OAuthRequestTokenTask to retrieve a request token and authorize | |
| 51 | * it by the user. After the request is authorized, this will get the callback. | |
| 52 | */ | |
| 53 | public class GetGlueAuthActivity extends BaseActivity {
| |
| 54 | public static final String GETGLUE_APIPATH_V2 = "http://api.getglue.com/v2/"; | |
| 55 | public static final String REQUEST_URL = "https://api.getglue.com/oauth/request_token"; | |
| 56 | public static final String ACCESS_URL = "https://api.getglue.com/oauth/access_token"; | |
| 57 | public static final String AUTHORIZE_URL = "http://getglue.com/oauth/authorize?style=mobile"; | |
| 58 | public static final String OAUTH_CALLBACK_SCHEME = "sgoauth"; | |
| 59 | public static final String OAUTH_CALLBACK_HOST = "getgluecallback"; | |
| 60 | public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST; | |
| 61 | ||
| 62 | public static final String OAUTH_TOKEN = "oauth_token"; | |
| 63 | public static final String OAUTH_TOKEN_SECRET = "oauth_token_secret"; | |
| 64 | ||
| 65 | ||
| 66 | final String TAG = "PrepareRequestTokenActivity"; | |
| 67 | ||
| 68 | private OAuthConsumer mConsumer; | |
| 69 | ||
| 70 | private OAuthProvider mProvider; | |
| 71 | ||
| 72 | private WebView mWebview; | |
| 73 | ||
| 74 | @Override | |
| 75 | public void onCreate(Bundle savedInstanceState) {
| |
| 76 | requestWindowFeature(Window.FEATURE_PROGRESS); | |
| 77 | super.onCreate(savedInstanceState); | |
| 78 | ||
| 79 | mWebview = new WebView(this); | |
| 80 | setContentView(mWebview); | |
| 81 | ||
| 82 | final ActionBar actionBar = getSupportActionBar(); | |
| 83 | actionBar.setTitle("OAUTH TITLE");
| |
| 84 | actionBar.setDisplayShowTitleEnabled(true); | |
| 85 | ||
| 86 | setSupportProgressBarVisibility(true); | |
| 87 | ||
| 88 | final SherlockFragmentActivity activity = this; | |
| 89 | mWebview.setWebChromeClient(new WebChromeClient() {
| |
| 90 | public void onProgressChanged(WebView view, int progress) {
| |
| 91 | /* | |
| 92 | * Activities and WebViews measure progress with different | |
| 93 | * scales. The progress meter will automatically disappear when | |
| 94 | * we reach 100%. | |
| 95 | */ | |
| 96 | activity.setSupportProgress(progress * 1000); | |
| 97 | } | |
| 98 | }); | |
| 99 | mWebview.setWebViewClient(new WebViewClient() {
| |
| 100 | public void onReceivedError(WebView view, int errorCode, String description, | |
| 101 | String failingUrl) {
| |
| 102 | Toast.makeText(activity, | |
| 103 | getString(R.string.getglue_authfailed) + " " + description, | |
| 104 | Toast.LENGTH_LONG).show(); | |
| 105 | ||
| 106 | finish(); | |
| 107 | } | |
| 108 | ||
| 109 | @Override | |
| 110 | public boolean shouldOverrideUrlLoading(WebView view, String url) {
| |
| 111 | if (url.startsWith(OAUTH_CALLBACK_URL)) {
| |
| 112 | Uri uri = Uri.parse(url); | |
| 113 | new RetrieveAccessTokenTask(mConsumer, mProvider, PreferenceManager | |
| 114 | .getDefaultSharedPreferences(activity)).execute(uri); | |
| 115 | ||
| 116 | finish(); | |
| 117 | return true; | |
| 118 | } | |
| 119 | return false; | |
| 120 | } | |
| 121 | - | */ }); |
| 121 | + | |
| 122 | ||
| 123 | this.mConsumer = new DefaultOAuthConsumer(Constants.GETGLUE_CONSUMER_KEY, | |
| 124 | Constants.GETGLUE_CONSUMER_SECRET); | |
| 125 | this.mProvider = new DefaultOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL); | |
| 126 | ||
| 127 | Log.i(TAG, "Starting task to retrieve request token."); | |
| 128 | new OAuthRequestTokenTask(this, mConsumer, mProvider, mWebview).execute(); | |
| 129 | } | |
| 130 | ||
| 131 | public static class OAuthRequestTokenTask extends AsyncTask<Void, String, String> {
| |
| 132 | final String TAG = "OAuthRequestTokenTask"; | |
| 133 | ||
| 134 | private Context mContext; | |
| 135 | private OAuthConsumer mConsumer; | |
| 136 | private OAuthProvider mProvider; | |
| 137 | private WebView mWebView; | |
| 138 | ||
| 139 | public OAuthRequestTokenTask(Context context, OAuthConsumer consumer, | |
| 140 | OAuthProvider provider, WebView webView) {
| |
| 141 | mContext = context; | |
| 142 | mConsumer = consumer; | |
| 143 | mProvider = provider; | |
| 144 | mWebView = webView; | |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * Retrieve the OAuth Request Token and present a browser to the user to | |
| 149 | * authorize the token. | |
| 150 | */ | |
| 151 | @Override | |
| 152 | protected String doInBackground(Void... params) {
| |
| 153 | try {
| |
| 154 | Log.i(TAG, "Retrieving request token from GetGlue servers"); | |
| 155 | String authUrl = mProvider.retrieveRequestToken(mConsumer, | |
| 156 | OAUTH_CALLBACK_URL); | |
| 157 | ||
| 158 | Log.i(TAG, "Popping a browser with the authorize URL"); | |
| 159 | publishProgress(authUrl); | |
| 160 | } catch (OAuthMessageSignerException e) {
| |
| 161 | Utils.trackExceptionAndLog(mContext, TAG, e); | |
| 162 | return e.getMessage(); | |
| 163 | } catch (OAuthNotAuthorizedException e) {
| |
| 164 | Utils.trackExceptionAndLog(mContext, TAG, e); | |
| 165 | return e.getMessage(); | |
| 166 | } catch (OAuthExpectationFailedException e) {
| |
| 167 | Utils.trackExceptionAndLog(mContext, TAG, e); | |
| 168 | return e.getMessage(); | |
| 169 | } catch (OAuthCommunicationException e) {
| |
| 170 | Utils.trackExceptionAndLog(mContext, TAG, e); | |
| 171 | return e.getMessage(); | |
| 172 | } | |
| 173 | return null; | |
| 174 | } | |
| 175 | ||
| 176 | @Override | |
| 177 | protected void onProgressUpdate(String... values) {
| |
| 178 | mWebView.loadUrl(values[0]); | |
| 179 | } | |
| 180 | ||
| 181 | @Override | |
| 182 | protected void onPostExecute(String result) {
| |
| 183 | if (result != null) {
| |
| 184 | Toast.makeText(mContext, result, Toast.LENGTH_LONG).show(); | |
| 185 | ((GetGlueAuthActivity) mContext).finish(); | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | } | |
| 190 | ||
| 191 | public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Integer> {
| |
| 192 | ||
| 193 | private static final int AUTH_FAILED = 0; | |
| 194 | private static final int AUTH_SUCCESS = 1; | |
| 195 | private SharedPreferences mPrefs; | |
| 196 | private OAuthProvider mProvider; | |
| 197 | private OAuthConsumer mConsumer; | |
| 198 | ||
| 199 | public RetrieveAccessTokenTask(OAuthConsumer consumer, OAuthProvider provider, | |
| 200 | SharedPreferences prefs) {
| |
| 201 | mPrefs = prefs; | |
| 202 | mProvider = provider; | |
| 203 | mConsumer = consumer; | |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * Retrieve the oauth_verifier, and store the oauth and | |
| 208 | * oauth_token_secret for future API calls. | |
| 209 | */ | |
| 210 | @Override | |
| 211 | protected Integer doInBackground(Uri... params) {
| |
| 212 | final Uri uri = params[0]; | |
| 213 | final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER); | |
| 214 | ||
| 215 | try {
| |
| 216 | mProvider.retrieveAccessToken(mConsumer, oauth_verifier); | |
| 217 | ||
| 218 | mPrefs.edit().putString(OAUTH_TOKEN, mConsumer.getToken()) | |
| 219 | .putString(OAUTH_TOKEN_SECRET, mConsumer.getTokenSecret()).commit(); | |
| 220 | ||
| 221 | Log.i(TAG, "OAuth - Access Token Retrieved"); | |
| 222 | return AUTH_SUCCESS; | |
| 223 | } catch (OAuthMessageSignerException e) {
| |
| 224 | Log.e(TAG, "OAuth - Access Token Retrieval Error", e); | |
| 225 | } catch (OAuthNotAuthorizedException e) {
| |
| 226 | Log.e(TAG, "OAuth - Access Token Retrieval Error", e); | |
| 227 | } catch (OAuthExpectationFailedException e) {
| |
| 228 | Log.e(TAG, "OAuth - Access Token Retrieval Error", e); | |
| 229 | } catch (OAuthCommunicationException e) {
| |
| 230 | Log.e(TAG, "OAuth - Access Token Retrieval Error", e); | |
| 231 | } | |
| 232 | ||
| 233 | return AUTH_FAILED; | |
| 234 | } | |
| 235 | ||
| 236 | @Override | |
| 237 | protected void onPostExecute(Integer result) {
| |
| 238 | switch (result) {
| |
| 239 | case AUTH_SUCCESS: | |
| 240 | break; | |
| 241 | case AUTH_FAILED: | |
| 242 | Toast.makeText(getApplicationContext(), getString(R.string.getglue_authfailed), | |
| 243 | Toast.LENGTH_LONG).show(); | |
| 244 | break; | |
| 245 | } | |
| 246 | } | |
| 247 | } | |
| 248 | ||
| 249 | } |