一、代码流程
1.MainActivity会开启PrepareRequestTokenActivity
2.PrepareRequestTokenActivity会根据配置文件的CONSUMER_KEY、CONSUMER_SECRET生成consumer
根据REQUEST_URL、ACCESS_URL、AUTHORIZE_URL生成provider,然后开启OAuthRequestTokenTask,执行异步操作,访问腾讯网络
3.在OAuthRequestTokenTask的doInBackground()中,provider.retrieveRequestToken()会指定回调地址,这里是指定“x-oauthflow://callback”
而在manifext.xml中,为PrepareRequestTokenActivity设置了<data android:scheme="x-oauthflow" android:host="callback"/>,所以一当OAuthRequestTokenTask成功返回后回调PrepareRequestTokenActivity,而PrepareRequestTokenActivity又设置了android:launchMode="singleTask",所以回调是是调用PrepareRequestTokenActivity的onNewIntent(),而不是onCreate()。
二、代码
1.xml
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn_launch_oauth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch OAuth Flow"
android:onClick="startPrepareRequestTokenActivity"/> <TextView android:id="@+id/response_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:typeface="normal"/>
</LinearLayout>
(2)AndroidManifest.xml
<activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="x-oauthflow" android:host="callback"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
2.java
(1)MainActivity.java
package org.oauth; import org.oauth.R; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void startPrepareRequestTokenActivity(View view) {
startActivity(new Intent().setClass(view.getContext(), PrepareRequestTokenActivity.class));
}
}
(2)PrepareRequestTokenActivity.java
package org.oauth; import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager; public class PrepareRequestTokenActivity extends Activity { private OAuthConsumer consumer;
private OAuthProvider provider; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL, Constants.ACCESS_URL, Constants.AUTHORIZE_URL);
new OAuthRequestTokenTask(this, consumer, provider).execute();
} //由于设置了<activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask">
//所以回调会调用此方法,而不要调用 onCreate()
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//最后获取的access_token,access_token_secet都存在prefs
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
final Uri uri = intent.getData();
System.out.println(uri.toString());
if (uri != null
&& uri.getScheme().equals(Constants.OAUTH_CALLBACK_SCHEME)) {
new RetrieveAccessTokenTask(this, consumer, provider, prefs)
.execute(uri);
finish();
}
}
}
(3)OAuthRequestTokenTask.java
package org.oauth; import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask; public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> { private Context context;
private OAuthConsumer consumer;
private OAuthProvider provider; public OAuthRequestTokenTask(Context context, OAuthConsumer consumer,
OAuthProvider provider) {
super();
this.context = context;
this.consumer = consumer;
this.provider = provider;
} @Override
protected Void doInBackground(Void... params) {
try {
System.out.println("请求Request Token之前" + consumer.getToken());
//retrieveRequestToken的第二个参数是回调URL
final String url = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL);
System.out.println("请求Request Token之后" + consumer.getToken());
System.out.println("url---->" + url);
Uri uri = Uri.parse(url);
//隐式的启动Activity
//tel://21983129863
//sms://767868698769
//https://.......会启动浏览器
Intent intent = new Intent(Intent.ACTION_VIEW, uri).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} }
(4)RetrieveAccessTokenTask.java
package org.oauth; import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log; public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> { final String TAG = "OAuth"; private Context context;
private OAuthProvider provider;
private OAuthConsumer consumer;
private SharedPreferences prefs; public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) {
this.context = context;
this.consumer = consumer;
this.provider = provider;
this.prefs=prefs;
} @Override
protected Void doInBackground(Uri...params) {
final Uri uri = params[0]; final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER); try {
provider.retrieveAccessToken(consumer, oauth_verifier);//会访问ACCESS_URL final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit(); String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); consumer.setTokenWithSecret(token, secret);
context.startActivity(new Intent(context,MainActivity.class)); Log.i(TAG, "OAuth - Access Token Retrieved"); } catch (Exception e) {
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
} return null;
}
}
(5)Constants.java
package org.oauth; public class Constants { //腾讯所分配的APP_KEY
public static final String CONSUMER_KEY = "99e9494ff07e42489f4ace16b63e1f47";
//腾讯所分配的APP_SECRET
public static final String CONSUMER_SECRET = "154f6f9ab4c1cf527f8ad8ab1f8e1ec9";
//用于获取未授权的request token
public static final String REQUEST_URL = "https://open.t.qq.com/cgi-bin/request_token";
//用于获取access token
public static final String ACCESS_URL = "https://open.t.qq.com/cgi-bin/access_token";
//用于对未授权的request token进行授权
public static final String AUTHORIZE_URL = "https://open.t.qq.com/cgi-bin/authorize"; public static final String ENCODING = "UTF-8"; //Manifest设置PrepareRequestTokenActivity时也有设置此项
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow";
public static final String OAUTH_CALLBACK_HOST = "callback";
//回调地址
public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST; }