第一步注册一个账户,并创建一个应用。获取app ID与 app Key。

第二步下载sdk

第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定。

3.1 修改清单文件,主要是加入一个webview的activity

[html]
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
3.2 将Android_SDK_v1.2.jar与httpmime-4.1.3.jar导入libs中就好。

3.3 在需要三方登录的地方,调用相应的api即可。

下面是小demo工程的清单文件及activity中api代码简单示例。
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.chesterweibodemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- 允许网络访问 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- demo activity,调用api -->
<activity
android:name=".ChesterWeiboDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- OAuth Version 2. 使用 WebView 辅助进行ImplicitGrant方式授权必须 -->
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.chesterweibodemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<!-- 允许网络访问 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- demo activity,调用api -->
<activity
android:name=".ChesterWeiboDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- OAuth Version 2. 使用 WebView 辅助进行ImplicitGrant方式授权必须 -->
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
ChesterWeiboDemoActivity 代码如下:
[java]
package com.test.chesterweibodemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
/**
* @author chensf5 2013-1-22
*/
public class ChesterWeiboDemoActivity extends Activity {
private static final String TAG = "ChesterWeiboDemoActivity";
private OAuthV2 oAuth;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//验证回调url地址,随便填写个 http://www.tencent.com/zh-cn/index.shtml
oAuth = new OAuthV2("http://apk.91.com/Soft/Android/com.palmit.player-1-1.0.html");
oAuth.setClientId("801297210"); // 2881064151
oAuth.setClientSecret("d163aeecdc7a9e5a601b03d66d4265be"); // be1dd1410434a9f7d5a2586bab7a6829
Intent intent = new Intent(ChesterWeiboDemoActivity.this,
OAuthV2AuthorizeWebView.class);
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, 1); //开启webview加载个html页面
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (null != msg.obj && msg.obj instanceof String) {
String response = (String) msg.obj;
((TextView) findViewById(R.id.tv_content)).setText(response
+ "\n");
Log.i(TAG, response);
Log.i(TAG + "----------",
"redirectUri:" + oAuth.getRedirectUri() + ",clientId:"
+ oAuth.getClientId() + ",clientSecret:"
+ oAuth.getClientSecret() + ",responseType:"
+ oAuth.getResponeType() + ",type:"
+ oAuth.getType() + ",authorizeCode:"
+ oAuth.getAuthorizeCode() + ",accessToken:"
+ oAuth.getAccessToken() + ",expiresIn:"
+ oAuth.getExpiresIn() + ",grantType:"
+ oAuth.getGrantType() + ",refreshToken:"
+ oAuth.getRefreshToken() + ",openid:"
+ oAuth.getOpenid() + "," + oAuth.getOpenkey());
}
};
};
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (requestCode == 1) {
if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
new Thread() {
public void run() {
oAuth = (OAuthV2) data.getExtras().getSerializable("oauth");
// 调用API获取用户信息
UserAPI userAPI = new UserAPI(
OAuthConstants.OAUTH_VERSION_2_A);
try {
String response = userAPI.info(oAuth, "json");// 获取用户信息
Message msg = handler.obtainMessage();
msg.obj = response;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
userAPI.shutdownConnection();
};
}.start();
} else {
Log.i(TAG, "返回过来的不对");
}
} else {
Log.i(TAG, "没有授权可拿");
}
}
}

demo工程的清单文件及activity中api代码简单示例的更多相关文章

  1. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  2. Windchill 配置LOG文件,使开发中的代码能显示打印的信息

    如开发代码的类HomeLogic.java, 包路径在pnt.report.home 需求:需监控此类的打印数据 方法:配置D:\ptc\Windchill_10.1\Windchill\codeba ...

  3. C#中汉字排序简单示例(拼音/笔划)

    可以按照区域语言修改排序规则. class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)&quo ...

  4. 关于SQL Server中存储过程在C#中调用的简单示例

    目录 0. 简介 1. 语法细节 2. 示例1:模拟转账 3. 示例2:测试返回DataTable 4. 源代码下载 shanzm-2020年5月3日 23:23:44 0. 简介 [定义]:存储过程 ...

  5. JAVA中CountDownLatch的简单示例

    public static void main(String[] args) throws InterruptedException { CountDownLatch latch =new Count ...

  6. Android的学习之路(四)项目中清单文件的学习和android中经常使用的显示单位

    1.所谓的清单文件就是项目中的AndroidManifest.xml文件.这个文件但是有大用处的.比方:app的名字,图标.app支持的版本号app的包名等等.以下我就介绍下这个清单文件的各个參数的作 ...

  7. 01_创建一个新的activity&activity配置清单文件

    今天开始学四大组件.今天是学Activity,然后是广播接收者,然后是服务,然后是内容提供者.四大组件,咱们一天一个.Activity就是跟用户交互的界面,大部分的应用都不会只有这么一个界面.创建多个 ...

  8. Android清单文件详解(三)----应用程序的根节点<application>

    <application>节点是AndroidManifest.xml文件中必须持有的一个节点,它包含在<manifest>节点下.通过<application>节 ...

  9. Android清单文件具体解释(三)----应用程序的根节点&lt;application&gt;

    <application>节点是AndroidManifest.xml文件里必须持有的一个节点,它包括在<manifest>节点下.通过<application>节 ...

随机推荐

  1. C#的Enum——枚举

    枚举 枚举类型声明为一组相关的符号常数定义了一个类型名称.枚举用于“多项选择”场合,就是程序运行时从编译时已经设定的固定数目的“选择”中做出决定. 枚举类型(也称为枚举)为定义一组可以赋给变量的命名整 ...

  2. Swift3.0语言教程获取字符

    Swift3.0语言教程获取字符 Swift3.0语言教程获取字符,在字符串中获取某一下标位置(下标索引)处的字符是很常见的功能,在NSString中使用character(at:)方法实现,其语法形 ...

  3. http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0

    http://www.roncoo.com/course/view/a09d8badbce04bd380f56034f8e68be0

  4. Codeforces Round #338 (Div. 2)

    水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...

  5. vsftpd 创建虚拟用户

    1.添加一个宿主用户:useradd vsftpd -s /sbin/nologin2.安装db4-utils,通过本底数据文件实现虚拟用户访问yum install db4-utils3.创建ftp ...

  6. iOS 关于iOS开发中的delegate

    有A.B两个对象,A要完成某件事,想让B帮它做. 这时候,A中就要实例化一个B的对象b,A还要在头文件中声明协议,然后在B中实现协议中对应的方法. 这时候再把A的delegate设置为b,在需要的地方 ...

  7. 【POJ】1811 Prime Test

    http://poj.org/problem?id=1811 题意:求n最小素因子.(n<=2^54) #include <cstdio> #include <cstring& ...

  8. POJ 2420 A Star not a Tree?(模拟退火)

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  9. 设置 tableview 的背景颜色,总是不生效

    1.只设置了背景图片,却忘记了取消掉 cell 的背景颜色(可以通过层次结构来观察) UIImageView *bgView = [[UIImageView alloc]initWithFrame:s ...

  10. fiddler 拦截小结

    一,拦截请求或响应常用命令 1.拦截命令 bpu 清除拦截请求 bpu http://www.baidu.com 拦截访问百度网站的请求.可以在 request 框的 WebForms 中改请求内容. ...