新浪微博API自动登录流程

微博 SDK 为开发者提供了 Oauth2.0 授权认证,并集成 SSO 登录功能,使第三方应用无需了解复杂的验证机制即可进行授权登录操作,并提供微博分享功能。

什么是Oauth?
OAuth is an open standard for authorization. OAuth provides client applications a ‘secure delegated access’ to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials[1].
什么是SSO?
SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统[2]。

登录步骤[3]:

申请APP KEY

在开放平台上注册应用即可获取。

注册应用程序的包名和签名

应用程序包名:指 AndroidManifest.xml 文件中,package 标签所代表的内容。
应用程序签名:该签名是通过官方提供的签名工具生成的 MD5 值。
什么是应用程序签名?
Android系统要求每一个Android应用程序必须要经过数字签名才能够安装到系统中,也就是说如果一个Android应用程序没有经过数字签名,是没有办法安装到系统中的。你没有给Android应用程序签名并不代表Android应用程序没有被签名。为了方便我们开发调试程序,ADT会自动的使用debug密钥为应用程序签名[4]。

导入jar包

导入weibosdkcore.jar或weibosdkcore_vx.x.x均可。

导入.so文件

将官方demo的libs目录下的三个文件夹中的.so文件,连同文件夹一起拷贝到项目中。
什么是.so文件?
A shared library or shared object is a file that is intended to be shared by executable files and further shared objects files. Modules used by a program are loaded from individual shared objects into memory at load time or run time, rather than being copied by a linker when it creates a single monolithic executable file for the program[5].

添加权限

 <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" /> 

创建微博授权类对象

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";
        AuthInfo mAuthInfo = new AuthInfo(this, "3254193153",
                "https://api.weibo.com/oauth2/default.html", SCOPE);

实现 WeiboAuthListener 接口

class AuthListener implements WeiboAuthListener {

        @Override
        public void onComplete(Bundle values) {
            mAccessToken = Oauth2AccessToken.parseAccessToken(values);
            System.out.println("token:" + mAccessToken.getToken());
            System.out.println("values:" + values.toString());
            if (mAccessToken.isSessionValid()) {

                Toast.makeText(WeiboActivity.this,
                        "weibosdk_demo_toast_auth_success", Toast.LENGTH_SHORT)
                        .show();
            } else {
                String code = values.getString("code");
                String message = "weibosdk_demo_toast_auth_failed";
                if (!TextUtils.isEmpty(code)) {
                    message = message + "\nObtained the code: " + code;
                }
                Toast.makeText(WeiboActivity.this, message, Toast.LENGTH_LONG)
                        .show();
            }
        }

        @Override
        public void onCancel() {
            Toast.makeText(WeiboActivity.this,
                    "weibosdk_demo_toast_auth_canceled", Toast.LENGTH_LONG)
                    .show();
        }

        @Override
        public void onWeiboException(WeiboException e) {
            Toast.makeText(WeiboActivity.this,
                    "Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
                    .show();
        }
    }

调用方法,认证授权

SsoHandler mSsoHandler = new SsoHandler(WeiboActivity.this, mAuthInfo);
         mSsoHandler.authorizeClientSso(new AuthListener());

实现OnActivityResult方法

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

你可能感兴趣的:(oauth)