ANDROID_MARS学习笔记_S04_001_OAUTH获取request_token
一、代码
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" 11 /> 12 <Button 13 android:id="@+id/getRequestTokenButtonId" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="获取未授权的Token" 17 /> 18 </LinearLayout>
(2)AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="org.marsdroid.oauth01" android:versionCode="1" 4 android:versionName="1.0"> 5 <uses-sdk android:minSdkVersion="10" /> 6 7 <uses-permission android:name="android.permission.INTERNET" /> 8 <uses-permission android:name="android.permission.READ_CONTACTS" /> 9 <application android:icon="@drawable/icon" android:label="@string/app_name"> 10 <activity android:name=".OAuth01Activity" android:label="@string/app_name"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 <category android:name="android.intent.category.LAUNCHER" /> 14 </intent-filter> 15 </activity> 16 17 <activity android:name=".PrepareRequestTokenActivity"> 18 <intent-filter> 19 <action android:name="android.intent.action.VIEW" /> 20 <category android:name="android.intent.category.DEFAULT" /> 21 <category android:name="android.intent.category.BROWSABLE" /> 22 <data android:scheme="x-oauthflow" android:host="callback" /> 23 </intent-filter> 24 </activity> 25 </application> 26 </manifest>
2.java
(1)OAuth01Activity.java
1 package org.marsdroid.oauth01; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 7 public class OAuth01Activity extends Activity { 8 9 10 /** Called when the activity is first created. */ 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.main); 15 startActivity(new Intent(this,PrepareRequestTokenActivity.class)); 16 } 17 18 }
(2)Constants.java
1 package org.marsdroid.oauth01; 2 3 4 public class Constants { 5 6 public static final String CONSUMER_KEY = "99e9494ff07e42489f4ace16b63e1f47"; 7 public static final String CONSUMER_SECRET = "154f6f9ab4c1cf527f8ad8ab1f8e1ec9"; 8 9 public static final String REQUEST_URL = "https://open.t.qq.com/cgi-bin/request_token"; 10 public static final String ACCESS_URL = "https://open.t.qq.com/cgi-bin/access_token"; 11 public static final String AUTHORIZE_URL = "https://open.t.qq.com/cgi-bin/authorize"; 12 13 public static final String ENCODING = "UTF-8"; 14 15 }
(3)PrepareRequestTokenActivity.java
1 package org.marsdroid.oauth01; 2 3 import oauth.signpost.OAuthConsumer; 4 import oauth.signpost.OAuthProvider; 5 import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 6 import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; 7 import android.app.Activity; 8 import android.os.Bundle; 9 10 public class PrepareRequestTokenActivity extends Activity { 11 12 private OAuthConsumer consumer; 13 private OAuthProvider provider; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 super.onCreate(savedInstanceState); 18 19 consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 20 provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL, Constants.ACCESS_URL, Constants.AUTHORIZE_URL); 21 22 new OAuthRequestTokenTask(this, consumer, provider).execute(); 23 } 24 25 }
(4)OAuthRequestTokenTask.java
用异步任务
1 package org.marsdroid.oauth01; 2 3 import oauth.signpost.OAuthConsumer; 4 import oauth.signpost.OAuthProvider; 5 import android.content.Context; 6 import android.os.AsyncTask; 7 8 public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void>{ 9 10 private Context context; 11 private OAuthConsumer consumer; 12 private OAuthProvider provider; 13 14 15 public OAuthRequestTokenTask(Context context, OAuthConsumer consumer, 16 OAuthProvider provider) { 17 super(); 18 this.context = context; 19 this.consumer = consumer; 20 this.provider = provider; 21 } 22 23 @Override 24 protected Void doInBackground(Void... params) { 25 // TODO Auto-generated method stub 26 try { 27 System.out.println("请求Request Token之前" + consumer.getToken()); 28 //用户授权要用此url 29 final String url = provider.retrieveRequestToken(consumer, "null"); 30 System.out.println("请求Request Toker之后" + consumer.getToken()); 31 System.out.println("url---->" + url); 32 } catch(Exception e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 return null; 37 } 38 39 }
You can do anything you set your mind to, man!