1、新建工程
先运行一下新建的工程;
2、在新浪开放平台上注册一个新的应用
http://open.weibo.com/apps/2803217617/info/basic
填写内容如下:
在左侧高级信息中填写如下信息:
3、根据开发文档进行jar包和so文件的添加
4、根据SDK开发文档一步一步的完成代码的编写
package com.zzh.weibologintext.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import com.zzh.sinaweibologintest.R;
public class MainActivity extends Activity {
private Button btn;
public static final String APP_KEY = "2803217617"; // 应用的APP_KEY
public static final String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";// 应用的回调页
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";
private AuthInfo mAuthInfo;
private SsoHandler mSsoHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
initEvent();
}
private void initEvent() {
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSsoHandler.authorize(new AuthListener());
}
});
}
private void initData() {
mAuthInfo = new AuthInfo(this, APP_KEY, REDIRECT_URL, SCOPE);
mSsoHandler = new SsoHandler(MainActivity.this, mAuthInfo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// SSO 授权回调
// 重要:发起 SSO 登陆的 Activity 必须重写 onActivityResults
if (mSsoHandler != null) {
mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
}
}
private void initView() {
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
}
class AuthListener implements WeiboAuthListener {
private Oauth2AccessToken mAccessToken;
@Override
public void onComplete(Bundle values) {
mAccessToken = Oauth2AccessToken.parseAccessToken(values);
// 从这里获取用户输入的 电话号码信息
String phoneNum = mAccessToken.getPhoneNum();
if (mAccessToken.isSessionValid()) {// 如果有效
// 保存 Token 到 SharedPreferences
// AccessTokenKeeper.writeAccessToken(WBAuthActivity.this,
// mAccessToken);
Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT)
.show();
System.out.println("得到的acccess_token:"
+ mAccessToken.getToken());
} else {// 无效
// 以下几种情况,您会收到 Code:
// 1. 当您未在平台上注册的应用程序的包名与签名时;
// 2. 当您注册的应用程序包名与签名不正确时;
// 3. 当您在平台上注册的包名和签名与您当前测试的应用的包名和签名不匹配时。
Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_LONG)
.show();
}
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_LONG).show();
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(MainActivity.this, "授权异常", Toast.LENGTH_LONG).show();
}
}
}
来自为知笔记(Wiz)
发表评论 取消回复