安卓仿制新浪微博(一)之OAuth2授权接口
这里需要用到请求授权(authorize)以及获取授权(access_token)
第一步:
将新浪的sdk放在src/libs下面
二:
//创建方法实现authorize
public void getauthorize(){
//获取实例
//实例化的方法一般有三种
//1.最常见的通过new;2.通过getinstence;3.通过工厂(Facetory)
Weibo weibo=Weibo.getInstence();//里面有三个参数.String appKey, String redirectUrl, String aScope,
//分别是你应用的appKey,网址,想要获取的权限可以把这些属性写在一个类里面设置成静态
//这里需要两个参数,一个为context,一个listenr
//context很好解决放的就是this,而listener需要的是WeiboAuthListener类型,我们没有所有就直接new一个
weibo.aututhorize(this,new WeiboAuthListener(){
//WeiboAuthListener里面自动生成4个方法
//抛出异常时
@Override
public void onWeiboException(WeiboException arg0) {
Log.d(TAG, "onWeiboException" + arg0); } // 出错时
@Override
public void onError(WeiboDialogError arg0) {
Log.d(TAG, "onError" + arg0); } // 成功时
@Override
public void onComplete(Bundle arg0) {//这里的Bundle arg0就是返回的code数据
Log.d(TAG, "onComplete" + arg0);
String code = arg0.getString("code");
//这是获得Toak的方法,在下面会写到
getToken(code);
} // 取消时
@Override
public void onCancel() {
Log.d(TAG, "onCancel====="); } });
}
现在已经获得code,现在我们需要通过code得到Token
//实现access_token获取已经授权的Access_token
public void getToken(){
//现在来填充参数
String url="https://api.weibo.com/oauth2/access_token";//注意这里不能有空格
WeiboParameters params=new WeiboParameters();
//params的中文就是参数的意思,所有将参数放在里面,所需的请求参数新浪API里面都有http://open.weibo.com/wiki/OAuth2/access_token
params.add("client_id", staticname.AppKey);//第一个是key,第二个是value
params.add("client_secret", staticname.App_Secret);
params.add("grant_type", "authorization_code");
params.add("code", code);
params.add("redirect_uri", staticname.RedirectUrl);
//第一步先写下面这个
AsyncWeiboRunner.request(url,params,"POST",new RequestListener(){
//自动生成四个方法
@Override
public void onIOException(IOException arg0) {
Log.d(TAG, "onIOException------" + arg0);
}
@Override
public void onError(WeiboException arg0) {
Log.d(TAG, "onError------" + arg0);
}
@Override
public void onComplete4binary(ByteArrayOutputStream arg0) {
Log.d(TAG, "onComplete4binary------" + arg0);
}
@Override
public void onComplete(String js) {// 得到Toak的就是js
Log.d(TAG, "onComplete------" + js);
//这里json解析的作用下面会讲到
Json json = new Json();
try {
json.pramas(js, Welcom.this);
handler.sendEmptyMessage(CODE_VIEWPAGER);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
)};//这里需要四个参数分别是String url, WeiboParameters params, String httpMethod, RequestListener listener
//第一个参数代表是新浪API里的所给网站,第二,四个参数没有和上面的方法一样new出来,第三个参数新浪API同样给出了是POST
}
现在已经获得Token了,那我们要实现如果用户已经授权过了之后,在token的有效期之前都不用重新授权的效果
这就需要将获得的Token解析并且使用SharedPreferences来存储
//新建一个类
//Token的返回数据请查看新浪微博API
public class Json{
//需要获取数据源,所以在方法上加了一个String js,至于为什么还要加一个context在Sharde会讲到
public void json(String js,Context context){
JsonObject json=new JsonObject(js);
//这里我们只需三个数据
String access_token = jsonObject.getString("access_token");
long expires_in = jsonObject.getLong("expires_in");//有效期
String uid = jsonObject.getString("uid");//用户id
Sharder sharder = new Sharder();
//将解析得到的数据存入SharedPreferences
sharder.putToke(context, access_token, expires_in);
sharder.putuid(context, uid);
} }
Json.class
//新建一个类来存储数据
public class Sharde(){ public void putToken(Context context,String token,long expires_in){
//要使用context调用get来获取SharedPreferences,而json里要使用此方法,所以json里面也要有context
SharedPreferences preferences=context.getSharedPreferences("token",SharedPreferences.Moden_PRI);//第一个是SharedPreferences的名字,第二个是数据存储形式
Editor editor= preferences.edit();
editor.putString("token",token);//因为这要放token的值所以要在方法里写上
editor.putLong("expires_in",expires_in);
//这个不要忘了
editor.commt;
}
public void putuid(Context context,String uid){
SharedPreferences preferences=context.getSharedPreferences("uid",SharedPreferences.Moden_PRI);
Editor editor= preferences.edit();
editor.putString("token",token);
//这个不要忘了
editor.commt;
}
//将SharedPreferences,的数据拿出来,以供判断使用
public Oauth2AccessToken getToken(){
SharedPreferences preferences=context.getSharedPreferences("token",SharedPreferences.Moden_PRI);
String token = preferences.getString("token", null);//第二个参数是默认值的意思
long expires_in = preferences.getLong("expires_in", 0);
return new Oauth2AccessToken(token,expires_in+"") ;//返回Token,里面不能放long型所以要转成String型
} public String getuid(){
SharedPreferences preferences = context.getSharedPreferences("uid",
context.MODE_PRIVATE);
String uid = preferences.getString("uid", null);
return uid;
} }
Sharde.class
现在我们要在主Activty里面判断
public void onClick(View v) {
if (sharder.getToke(Welcom.this).isSessionValid()) {
Intent intent=new Intent(Welcom.this,Viewpager.class);
startActivity(intent);//这是实现跳转的,下篇文章再说
return;
}
getcode();
}
});
最后不要忘记注册Activty和权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这并不是一篇教程,仅仅是我给老师教的作业,代码也不是在eclipse里面打的,里面可能有很多错误,希望大家多多见谅,源代码明天再上传,今天太累了
安卓仿制新浪微博(一)之OAuth2授权接口的更多相关文章
- 微信OAuth2.0网页授权接口
微信OAuth2.0网页授权接口 微信OAuth2.0网页授权接口的thinkphp实现版本号.主要实现了oauth网页受权,以及部分其它接口. 用法 为什么用OAuth2.0受权? 通过OAuth2 ...
- [iOS微博项目 - 2.1] - 获得新浪授权接口
A.如何获得新浪的授权接口 登陆新浪的开放平台 注册新浪账号 创建应用 获得应用id和请求地址 查阅相关API 关联需要进行测试的账号 1.登陆开放平台 http://open.weibo.com ...
- [认证授权] 2.OAuth2授权(续) & JWT(JSON Web Token)
1 RFC6749还有哪些可以完善的? 1.1 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何获得 ...
- 微信企业号OAuth2验证接口实例(使用SpringMVC)
微信企业号OAuth2验证接口(使用SpringMVC) 企业应用中的URL链接(包含自己定义菜单或者消息中的链接).能够通过OAuth2.0来获取员工的身份信息. 注意.此URL的域名,必须全然匹配 ...
- Spring Security OAuth2 授权码模式
背景: 由于业务实现中涉及到接入第三方系统(app接入有赞商城等),所以涉及到第三方系统需要获取用户信息(用户手机号.姓名等),为了保证用户信息的安全和接入方式的统一, 采用Oauth2四种模式之一 ...
- Spring Security OAuth2 授权失败(401) 问题整理
Spring Cloud架构中采用Spring Security OAuth2作为权限控制,关于OAuth2详细介绍可以参考 http://www.ruanyifeng.com/blog/2014/0 ...
- 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法
利用Laravel 搭建oauth2 API接口 要求 laravel 5.4以上 安装 $ composer require laravel/passport 在配置文件 config/app.ph ...
- 微信授权就是这个原理,Spring Cloud OAuth2 授权码模式
上一篇文章Spring Cloud OAuth2 实现单点登录介绍了使用 password 模式进行身份认证和单点登录.本篇介绍 Spring Cloud OAuth2 的另外一种授权模式-授权码模式 ...
- Spring Security 实战干货:客户端OAuth2授权请求的入口
1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...
随机推荐
- android AudioRecorder简单心得
1.如何创建一个有效的AudioRecorder实例 Android各种设备的采样频率不同,输入的声道数也不同,如果采用固定的采样频率和声道数,那么得到的AudioRecorder不一定能够正常初始化 ...
- 使用objdump objcopy查看与修改符号表
使用objdump objcopy查看与修改符号表动态库Linuxgccfunction 我们在 Linux 下运行一个程序,有时会无法启动,报缺少某某库.这时需要查看可执行程序或者动态库中的符 ...
- U-Boot启动过程完全分析
U-Boot启动过程完全分析 1.1 U-Boot工作过程 U-Boot启动内核的过程可以分为两个阶段,两个阶段的功能如下: (1)第一阶段的功能 硬件设备初始化 加载U-Boot第二阶段 ...
- css属性之transition
浏览器支持 Internet Explorer 10.Firefox.Opera 和 Chrome 支持 transition 属性. Safari 支持替代的 -webkit-transition ...
- Android入门学习:Android 系统框架及应用程序执行过程
Android基础知识学习 新手上路,还请多多帮助.由于初学,博客内容难免有不正确的地方,还请各位多多指教,相互学习! 主要内容: 1.Android层次架构及主要功能 2.Android编程模型,程 ...
- HDU_2018——母牛产小牛的问题,递推
Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多 ...
- Objective-C priority queue
http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...
- 项目开发中封装一个BarButtonItem类别-很实用
Encapsulates a TabBarItem--封装一个BarButtonItem类 在我们程序的导航栏的左边或右边一般都会有这样的BarButtonItem,用来界面之间的跳转 如果我们有很多 ...
- SQL Server error "Xml data type is not supported in distributed queries" and workaround for it
Recently while working with data migration,got an error while running a following query where Server ...
- Guzzle php resetful webservice farmework
Guzzle is a PHP HTTP client that makes it easy to work with HTTP/1.1 and takes the pain out of consu ...