package com.ui;
import com.tencent.weibo.api.TAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv1.OAuthV1;
import com.tencent.weibo.oauthv1.OAuthV1Client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.tencent.weibo.webview.OAuthV1AuthorizeWebView;
public class WeiBotestsActivity extends Activity {
private Button btn_request_token,btn_request_access,btn_request_webview,btn_weibo_test;
private EditText edt_weibo_test;
private TextView text_request_token,text_request_access,text_request_webview,text_weibo_test;
private OAuthV1 oAuth;
private String key = "801353796";
private String secret = "676b1f34dd40fb22830dc9e23485e2f3";
private String oauthCallback = "null";
private String token, token_secret;// 保存认证后的token,token_secret。
private final String TOKEN_KEY = "token";
private final String TOKEN_SECRET_KEY = "token_secret";
private SharedPreferences preferences;
public static String file = "tokenfile";
private Editor editor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取控件
btn_request_token = (Button) findViewById(R.id.btn_request_token);
btn_request_access = (Button) findViewById(R.id.btn_request_access);
btn_request_webview = (Button) findViewById(R.id.btn_request_webview);
btn_weibo_test = (Button) findViewById(R.id.btn_weibo_test);
edt_weibo_test = (EditText) findViewById(R.id.text_weibo_test);
btn_request_token.setOnClickListener(listen);
btn_request_access.setOnClickListener(listen);
btn_request_webview.setOnClickListener(listen);
btn_weibo_test.setOnClickListener(listen);
text_request_token = (TextView) findViewById(R.id.text_request_token);
text_request_access = (TextView) findViewById(R.id.text_request_access);
text_request_webview = (TextView) findViewById(R.id.text_request_webview);
// text_weibo_test = (TextView) findViewById(R.id.text_weibo_test);
preferences = getSharedPreferences(file, Context.MODE_PRIVATE);
token = preferences.getString(TOKEN_KEY, "");
token_secret = preferences.getString(TOKEN_SECRET_KEY, "");
//将申请的key 和 secret
oAuth = new OAuthV1(oauthCallback);
oAuth.setOauthConsumerKey(key);
oAuth.setOauthConsumerSecret(secret);
if (token != null && !token.equals("") && !token_secret.equals("")
&& token_secret != null) {
// button.setVisibility(View.GONE);
// but_custom.setVisibility(View.GONE);
// 将认证后的值set进去就可以调用了.
oAuth.setOauthToken(token);
oAuth.setOauthTokenSecret(token_secret);
System.out.println("token=" + token);
System.out.println("token_secret=" + token_secret);
}
}
OnClickListener listen = new OnClickListener() {
public void onClick(View v) {
Intent intent = null;
TAPI tAPI = null;
String response = null;
switch(v.getId()){
case R.id.btn_request_token:
// 向腾讯微博开放平台请求获得未授权的Request_Token
try {
oAuth = OAuthV1Client.requestToken(oAuth);
} catch (Exception e) {
e.printStackTrace();
}
// 创建Intent,使用WebView让用户授权
intent = new Intent(WeiBotestsActivity.this,
OAuthV1AuthorizeWebView.class);
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, 1);
String t = oAuth.getOauthToken();
String ts = oAuth.getOauthTokenSecret();
Log.i("oAuth-------------", t+"----------------"+ts);
break;
case R.id.btn_request_webview:
try {
oAuth = OAuthV1Client.requestToken(oAuth);
} catch (Exception e) {
e.printStackTrace();
}
intent = new Intent(WeiBotestsActivity.this,
MyTencentWebViewActivity.class);
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, MyTencentWebViewActivity.RESULT_CODE);
break;
case R.id.btn_request_access:
Toast.makeText(WeiBotestsActivity.this, "access", 5000).show();
break;
case R.id.btn_weibo_test:
//发送微博测试
String content = edt_weibo_test.getText().toString();
if (token == null || token.equals("")) {
Toast.makeText(WeiBotestsActivity.this, "请先Oauth认证", 1000).show();
} else {
if (content.equals("")) {
Toast.makeText(WeiBotestsActivity.this, "内容不能为空", 1).show();
} else {
tAPI = new TAPI(OAuthConstants.OAUTH_VERSION_1);
try {
response = tAPI
.add(oAuth, "json", content, "127.0.0.1");
} catch (Exception e) {
e.printStackTrace();
}
if (response != null)
Toast.makeText(WeiBotestsActivity.this, "发表成功", 1).show();
tAPI.shutdownConnection();
}
}
break;
default:
break;
}
}
};
/*
* 通过读取OAuthV1AuthorizeWebView返回的Intent,获取用户授权后的验证码
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == OAuthV1AuthorizeWebView.RESULT_CODE) {
// button.setVisibility(View.GONE);
// but_custom.setVisibility(View.GONE);
// 从返回的Intent中获取验证码
oAuth = (OAuthV1) data.getExtras().getSerializable("oauth");
/*
* 注意:此时oauth中的Oauth_token和Oauth_token_secret将发生变化,用新获取到的
* 已授权的access_token和access_token_secret替换之前存储的未授权的request_token
* 和request_token_secret.
*/
// 在这里要保存access_token,access_token_secret
try {
oAuth = OAuthV1Client.accessToken(oAuth);
token = oAuth.getOauthToken();
token_secret = oAuth.getOauthTokenSecret();
preferences = getSharedPreferences(file, Context.MODE_PRIVATE);
editor = preferences.edit();
editor.putString(TOKEN_KEY, token);
editor.putString(TOKEN_SECRET_KEY, token_secret);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android 腾讯微博Oauth2.0认证以及自定义webview认证
共36个文件
class:10个
png:6个
jar:5个
5星 · 超过95%的资源 需积分: 33 18 下载量 134 浏览量
2013-05-10
11:47:32
上传
评论 1
收藏 808KB ZIP 举报
温馨提示
android 腾讯微博Oauth2.0认证以及自定义webview认证
资源推荐
资源详情
资源评论
收起资源包目录
weiBotests.zip (36个子文件)
weiBotests
bin
classes.dex 74KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
weiBotests.apk 45KB
classes
com
ui
MyTencentWebViewActivity$1.class 2KB
MyTencentWebViewActivity.class 3KB
R$layout.class 391B
R$id.class 717B
R$drawable.class 371B
WeiBotestsActivity$1.class 3KB
R$attr.class 304B
R.class 447B
WeiBotestsActivity.class 5KB
R$string.class 391B
resources.ap_ 12KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 186B
drawable-mdpi
ic_launcher.png 3KB
layout
mywebview.xml 790B
main.xml 2KB
assets
gen
com
ui
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 177B
src
com
ui
WeiBotestsActivity.java 6KB
MyTencentWebViewActivity.java 2KB
.project 846B
.classpath 590B
lib
dom4j-1.6.1.jar 307KB
QWeiboSDK.jar 40KB
commons-httpclient-3.1.jar 298KB
commons-logging-1.1.jar 52KB
commons-codec.jar 46KB
project.properties 361B
AndroidManifest.xml 998B
共 36 条
- 1
资源评论
- ygh0000000012014-03-07这个 资源非常好,适合下载学习!
- qa48627412014-10-22正好现在在学android,非常好的demo
mingtingjie
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 校园帮项目,毕业设计/课程设计/javaWeb/SSM
- C++ plotting library,matplotlib-cpp-master.zip
- 案例源码matplotlib-examples-master.zip
- 基于JavaScript 实现的KMP 算法
- 基于C++实现二叉树的创建,遍历,添加,查找与删除
- 基于C语言实现二叉树的基本操作
- 毕业设计基于STM32的测量温度与压力的数据处理设计C语言完整源码+论文.zip
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- Vision Transformer 网络对不同氨气氧气浓度轨迹RAS 图像数据集的分类,包含训练权重和数据集、迁移学习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功