android使用新浪微博最新SDK4.1进行第三方授权登录

移动开发 waitig 898℃ 百度已收录 0评论

android使用新浪微博最新SDK进行第三方授权登录


工具:android-studio
新浪SDK版本:4.1

1Demo参考
下载官方SDK:https://github.com/sinaweibosdk/weibo_android_sdk
gitHub项目集成到android-studio请参考博客:
用android-studio打开weibosdkdemo(同步了github的项目后,可以关闭该项目,重新打开只有weibosdkdemo的文件夹)
按Message去下载支持的gradle版本即可运行


2 集成

1 在project的build.gradle中,添加库

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://dl.bintray.com/thelasterstar/maven/" }
    }
}

2 在app的build.gradle中,添加依赖

dependencies {
    ...
 compile 'com.sina.weibo.sdk:core:4.1.0:openDefaultRelease@aar'
 }

3 新建一个LoginActivity的Activity类
3.1

private void initWeiBoSDK() {
        //新浪微博初始化,对应的参数分别是app_key,回调地址,和Scope权限(这里默认使用demo中的)
        WbSdk.install(this, new AuthInfo(this, "app_key", "回调地址",
                "email,direct_messages_read,direct_messages_write,"
                        + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
                        + "follow_app_official_microblog," + "invitation_write"));
    }

3.2

//创建SsoHandler对象
mSsoHandler = new SsoHandler(LoginActivity.this);

3.3

 private void loginIn() {
        mSsoHandler.authorize(new SelfWbAuthListener());
    }

mSsoHandler.authorize:若安装了客户端,则通过客户端授权,若没有安装,则通过WEB方式授权(建议移动开发使用这种模式)
mSsoHandler.authorizeWeb:只通过web方式授权
mSsoHandler.authorizeClientSso:只通过新浪微博客户端授权


3.4 核心

private class SelfWbAuthListener implements com.sina.weibo.sdk.auth.WbAuthListener {
      @Override
      public void onSuccess(final Oauth2AccessToken token) {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  mAccessToken = token;
                  if (mAccessToken.isSessionValid()) {
                      // 显示 Token
                //updateTokenView(false);
                      // 保存 Token 到 SharedPreferences
                //  AccessTokenKeeper.writeAccessToken(LoginActivity.this, mAccessToken);
                      Log.i("获取mAccessToken成功","获取mAccessToken成功");
                      Log.i("mAccessToken为:", String.valueOf(mAccessToken));

                  }
              }
          });
      }

      @Override
      public void cancel() {
          Log.i("取消授权","取消授权");
      }

      @Override
      public void onFailure(WbConnectErrorMessage errorMessage) {
          Log.i("授权失败","授权失败");
      }
  }

3.5要获得回传信息必须要重写该方法

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //sina login
        if(mSsoHandler!=null){
            mSsoHandler.authorizeCallBack(requestCode,resultCode,data);
        }
    }

完整代码参考:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WbConnectErrorMessage;
import com.sina.weibo.sdk.auth.sso.SsoHandler;

public class LoginActivity extends AppCompatActivity {
    private SsoHandler mSsoHandler;
    private Oauth2AccessToken mAccessToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initWeiBoSDK();
        mSsoHandler = new SsoHandler(LoginActivity.this);
        loginIn();
    }
    /**
     *       SSO授权方式获取权限
     */
    private void initWeiBoSDK() {
        //新浪微博初始化,对应的参数分别是app_key,回调地址,和权限
        WbSdk.install(this, new AuthInfo(this, "你的app_key", "https://api.weibo.com/oauth2/default.html",
                "email,direct_messages_read,direct_messages_write,"
                        + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
                        + "follow_app_official_microblog," + "invitation_write"));
    }

    private void loginIn() {
        mSsoHandler.authorize(new SelfWbAuthListener());
    }

    private class SelfWbAuthListener implements com.sina.weibo.sdk.auth.WbAuthListener {
        @Override
        public void onSuccess(final Oauth2AccessToken token) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mAccessToken = token;
                    if (mAccessToken.isSessionValid()) {
                        // 显示 Token
//                        updateTokenView(false);
                        // 保存 Token 到 SharedPreferences
//                        AccessTokenKeeper.writeAccessToken(LoginActivity.this, mAccessToken);
                        Log.i("获取mAccessToken成功","获取mAccessToken成功");
                        Log.i("mAccessToken为:", String.valueOf(mAccessToken));

                    }
                }
            });
        }

        @Override
        public void cancel() {
            Log.i("取消授权","取消授权");
        }

        @Override
        public void onFailure(WbConnectErrorMessage errorMessage) {
            Log.i("授权失败","授权失败");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //sina login
        if(mSsoHandler!=null){
            mSsoHandler.authorizeCallBack(requestCode,resultCode,data);
        }
    }
}

附录:
新浪微博android4.1SDK参考文档:
https://github.com/sinaweibosdk/weibo_android_sdk/blob/master/%E6%96%B0%E6%96%87%E6%A1%A3/%E5%BE%AE%E5%8D%9ASDK%204.1%E6%96%87%E6%A1%A3.pdf
OAuth授权具体流程(新浪微博非第三方SDK集成授权)参考:
http://blog.csdn.net/weixin_37577039/article/details/78632112


本文由【waitig】发表在等英博客
本文固定链接:android使用新浪微博最新SDK4.1进行第三方授权登录
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)