首先去http://open.weibo.com注册一个开发账号。
然后从页面上面的Menu中选择“应用开发” -> "移动应用" -> 创建应用
填写好应用的基本信息后,再去“管理应用”页面,再修改应用的基本信息与高级信息,主要是修改redirect_url 与 android包名 ,android签名等信息 。
要注意的是redirect_url andriod报名 签名等信息都需要与程序中一致
首先需要加入sina提供的android sdk包 ,下载地址:https://github.com/mobileresearch/weibo_android_sdk
下面是编码
1、常量类
package com.weibo.nv.sina; public interface ConstantS { // 应用的key appkey public static final String APP_KEY = "3972613607"; public static final String REDIRECT_URL = "http://www.baidu.com"; // 新支持scope:支持传入多个scope权限,用逗号分隔 public static final String SCOPE = "email,direct_messages_read,direct_messages_write," + "friendships_groups_read,friendships_groups_write,statuses_to_me_read," + "follow_app_official_microblog," + "invitation_write"; public static final String CLIENT_ID = "client_id"; public static final String RESPONSE_TYPE = "response_type"; public static final String USER_REDIRECT_URL = "redirect_uri"; public static final String DISPLAY = "display"; public static final String USER_SCOPE = "scope"; public static final String PACKAGE_NAME = "packagename"; public static final String KEY_HASH = "key_hash"; }
2、MainActivity (用户发起授权请求)
package com.weibo.nv.sina; import java.text.SimpleDateFormat; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import com.weibo.sdk.android.Oauth2AccessToken; import com.weibo.sdk.android.Weibo; import com.weibo.sdk.android.WeiboAuthListener; import com.weibo.sdk.android.WeiboDialogError; import com.weibo.sdk.android.WeiboException; import com.weibo.sdk.android.util.AccessTokenKeeper; public class MainActivity extends Activity { /** 微博API接口类,提供登陆等功能 */ private Weibo mWeibo; /** 封装了 "access_token","expires_in","refresh_token",并提供了他们的管理功能 */ private Oauth2AccessToken mAccessToken; @SuppressLint("SimpleDateFormat") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWeibo = Weibo.getInstance(ConstantS.APP_KEY, ConstantS.REDIRECT_URL, ConstantS.SCOPE); findViewById(R.id.loginBySina).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i("test" , "on click"); mWeibo.anthorize(MainActivity.this, new AuthDialogListener()); } }); mAccessToken = AccessTokenKeeper.readAccessToken(this); //获取Token信息(如果上次授权成功,并保存了Token信息,那本本次可以直接使用本方式获取Token信息) if (mAccessToken.isSessionValid()) { //判断是否登录成功 String date = new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss") .format(new java.util.Date(mAccessToken.getExpiresTime())); String info = "access_token 仍在有效期内,无需再次登录: \naccess_token:" + mAccessToken.getToken() + "\n有效期:" + date ; Toast.makeText( MainActivity.this , info , Toast.LENGTH_LONG).show() ; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } class AuthDialogListener implements WeiboAuthListener { /** * 授权成功后会触发该方法,并且会将授权的信息放入Bundle中 */ @SuppressLint("SimpleDateFormat") @Override public void onComplete(Bundle values) { String token = values.getString("access_token"); String expires_in = values.getString("expires_in"); mAccessToken = new Oauth2AccessToken(token, expires_in); if (mAccessToken.isSessionValid()) { String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") .format(new java.util.Date(mAccessToken.getExpiresTime())); String info = "认证成功: \r\n access_token: " + token + "\r\n" + "expires_in: " + expires_in + "\r\n有效期:" + date; AccessTokenKeeper.keepAccessToken(MainActivity.this, mAccessToken); //将登录信息保存,下次可以直接获取 Log.i("test", info); Toast.makeText( MainActivity.this , info , Toast.LENGTH_LONG).show() ; }else{ Toast.makeText(MainActivity.this, "认证失败", Toast.LENGTH_SHORT).show(); } } @Override public void onError(WeiboDialogError e) { Toast.makeText(getApplicationContext(), "Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show(); } @Override public void onCancel() { Toast.makeText(getApplicationContext(), "Auth cancel", Toast.LENGTH_LONG).show(); } @Override public void onWeiboException(WeiboException e) { Toast.makeText(getApplicationContext(), "Auth exception : " + e.getMessage(), Toast.LENGTH_LONG).show(); } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.weibo.nv.sina" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.weibo.nv.sina.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
在MainActivity中授权成功后会回掉AuthDialogListener的onComplete方法,并且授权信息保存在参数values中。
可以将首选信息保存下来,在授权信息未过期前直接使用,不需要用户再次登录, 可以使用AccessTokenKeeper.keepAccessToken(MainActivity.this, mAccessToken); 将登录信息保存 , 下次获取时使用AccessTokenKeeper.readAccessToken(this); 就可以获取授权信息。
也可以将授权信息保存到数据库或文件。 或发送给其他服务器使用。 只需要access_token即可。
发布一条微博的请求如下:
url:https://api.weibo.com/2/statuses/update.json
method:post
contentType:application/x-www-form-urlencoded; charset=UTF-8
requestBody:status=测试微博内容&access_token=2.00Ot8XJEBRgq1E9262e9bf4eQs4SJD