一、代码

1.xml
(1)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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/getRequestTokenButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取未授权的Token"
/>
</LinearLayout>

(2)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.marsdroid.oauth01" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".OAuth01Activity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".PrepareRequestTokenActivity">
<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>
</manifest>

2.java
(1)OAuth01Activity.java

 package org.marsdroid.oauth01;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; public class OAuth01Activity extends Activity { /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this,PrepareRequestTokenActivity.class));
} }

(2)Constants.java

 package org.marsdroid.oauth01;

 public class Constants {

     public static final String CONSUMER_KEY     = "99e9494ff07e42489f4ace16b63e1f47";
public static final String CONSUMER_SECRET = "154f6f9ab4c1cf527f8ad8ab1f8e1ec9"; public static final String REQUEST_URL = "https://open.t.qq.com/cgi-bin/request_token";
public static final String ACCESS_URL = "https://open.t.qq.com/cgi-bin/access_token";
public static final String AUTHORIZE_URL = "https://open.t.qq.com/cgi-bin/authorize"; public static final String ENCODING = "UTF-8"; }

(3)PrepareRequestTokenActivity.java

 package org.marsdroid.oauth01;

 import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import android.app.Activity;
import android.os.Bundle; public class PrepareRequestTokenActivity extends Activity { private OAuthConsumer consumer;
private OAuthProvider provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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();
} }

(4)OAuthRequestTokenTask.java

用异步任务

 package org.marsdroid.oauth01;

 import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import android.content.Context;
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) {
// TODO Auto-generated method stub
try {
System.out.println("请求Request Token之前" + consumer.getToken());
//用户授权要用此url
final String url = provider.retrieveRequestToken(consumer, "null");
System.out.println("请求Request Toker之后" + consumer.getToken());
System.out.println("url---->" + url);
} catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} }

ANDROID_MARS学习笔记_S04_001_OAUTH获取request_token的更多相关文章

  1. ANDROID_MARS学习笔记_S03_001_获取蓝牙匹配列表

    一.代码 1.xml(1)AndroidManifest.xml 增加 <uses-permission android:name="android.permission.BLUETO ...

  2. ANDROID_MARS学习笔记_S04_001_OAuth简介

    一.OAuth简介

  3. openresty 学习笔记二:获取请求数据

    openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...

  4. ANDROID_MARS学习笔记_S04_006_用获取access_token,access_token_secrect

    一.代码流程 1.MainActivity会开启PrepareRequestTokenActivity 2.PrepareRequestTokenActivity会根据配置文件的CONSUMER_KE ...

  5. ANDROID_MARS学习笔记_S05_001_用SensorManager获取传感器

    1. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  6. ANDROID_MARS学习笔记_S04_007_从服务器获取微博数据时间线

    一.代码 1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  7. JDBC学习笔记(6)——获取自动生成的主键值&处理Blob&数据库事务处理

    获取数据库自动生成的主键 [孤立的技术是没有价值的],我们这里只是为了了解具体的实现步骤:我们在插入数据的时候,经常会需要获取我们插入的这一行数据对应的主键值. 具体的代码实现: /** * 获取数据 ...

  8. struts2学习笔记--总结获取servletAPI的几种方式

    struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...

  9. IOS学习笔记之获取Plist文件读取数据

    @property(nonatomic,strong) NSArray *pic; //创建数组属性 @property(nonatomic,assign) int index; //创建索引属性 @ ...

随机推荐

  1. JavaScript如何获得Select下拉框选中的值

    js代码: var mySelect = document.getElementById("resultList2"); var mySelectText = mySelect.o ...

  2. JQ实现复选框的全选反选不选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Servlet & JSP - Decorating Requests and Responses

    Servlet API 提供了四个包装类:ServletRequestWrapper.ServletResponseWrapper.HttpServletRequestWrapper.HttpServ ...

  4. js获得文件根目录

    function getRootPath(){ //获取当前网址,如: http://localhost:8083/proj/meun.jsp var curWwwPath = window.docu ...

  5. 浅谈Javase内存流程图

    最近接触OOP面向对象,学习了OOP.this.super.package.extends还有override,整体来说如果不清楚内存的流程的话,对这些知识会很混淆,在老师的帮助下,花了一张图,能清楚 ...

  6. frame 第三节

    1.准备3个文件 main.html: <html> <head> <title>框架</title> </head> <frames ...

  7. 命令行下上传文件到iOS软件 专业文件管理/gplayer

    U盘丢了, 就拿手机当U盘用用先. 一般情况下软件打开上传功能, 在浏览器里上传即可. 可是偏偏我的电影放在了 树莓派里面(搭建了一个SMB), 直接浏览器的话,会多占用些带宽, 我的破路由器.... ...

  8. notepad++插件使用说明

    在进行渗透测试的过程中,经常需要分析一些js或xml文件,或者是一些url,它们多是经过压缩或编码的,看起来不方便.这里介绍几款常用的notepad++插件,会极大的方便我们的开发测试. 1 Xml ...

  9. C#实现记事本查找功能

    private void button1_Click(object sender, EventArgs e) { if (!(String.IsNullOrEmpty(this.textBox1.Te ...

  10. echo、print、print_r、printf、sprintf、var_dump的区别比较

    一.echoecho() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误.而且echo是返回void的,并不 ...