Weibo Android SDK

repository·master·Indexed 20 days ago

https://github.com/mobileresearch/weibo_android_sdk

A toolkit for Android developers to integrate Weibo authentication (OAuth 2.0 and SSO) and sharing capabilities. Version 2.4.0 provides unified package names under com.sina.weibo.sdk, refactored authorization and sharing modules, and UI components for one-click login. The SDK includes a closed-source core (weibosdkcore.jar) for basic auth and sharing, and an open-source library for full OpenAPI access and login/logout controls.

Tokens
2.6K
Snippets
5
Records
10
Agent score
21%

What's inside weibo_android_sdk

  1. SDK functional capabilities

    master

    The Weibo SDK provides the following core functionalities:

    1. Authentication & Authorization

    Supports Oauth2.0 Web Authorization and SSO (Single Sign-On) Login. SSO allows users to log in by invoking the Weibo client. Note: SSO requires the Weibo client to be installed on the device.

    2. Weibo Sharing

    Allows sharing text, images, videos, and music. There are two modes:

    • Third-party app invokes Weibo client: The standard method for most apps.
    • Weibo client invokes third-party app: Requires specific cooperation/integration. Note: The SDK currently only supports sharing via the official Weibo client.

    3. Login/Logout Controls

    Provides two types of UI components:

    • One-click login button
    • Login/Logout button Both use the SSO login interface. Logout is handled via encapsulated OpenAPI interfaces.

    4. OpenAPI

    Provides a framework for calling Weibo services, including:

    • Logout Interface: An authorization recovery interface to revoke user authorization.
    • Invitation Interface: Allows logged-in users to send private message invitations or gifts to their Weibo followers.
  2. Understand the Weibo SDK project structure

    master

    The SDK is provided in a partially open-source format consisting of three parts:

    1. Closed-source (weibosdkcore.jar): Contains core functionality for authorization, SSO login, and sharing. Use this if you only need authorization and sharing.
    2. Open-source (WeiboSDK Library): A library that references weibosdkcore.jar. It wraps the OpenAPI and provides login/logout controls. Use this if you need the full feature set including OpenAPI and UI controls.
    3. Demo (WeiboSDKDemo): An example project that references the WeiboSDK library, demonstrating all supported features.
  3. Core concepts and terminology

    master

    Understanding these key terms is essential for using the SDK:

    TermDescription
    AppKeyA unique key assigned to your third-party application for authentication and identity.
    RedirectURIThe authorization callback page. For mobile clients, this is invisible to the user. It is recommended to use the default: https://api.weibo.com/oauth2/default.html.
    ScopeAn OAuth2.0 feature that allows developers to request specific core Weibo functions while enhancing user privacy.
    AccessTokenA token representing the user's identity, used for calling Weibo APIs.
    Expire inThe expiration time used to determine if a login session has expired.
    Oauth2.0 Web AuthorizationAuthorization performed via a WebView, returning Token information.
    SSO AuthorizationSingle Sign-On authorization that invokes the Weibo client to return Token information. (Requires the Weibo client to be installed).
  4. Breaking changes in Android SDK V2.4.0

    master

    Version 2.4.0 introduced several significant changes:

    • Package Name: Unified to start with com.sina.weibo.sdk.
    • Interface Renaming:
      • Authorization: Weibo $\rightarrow$ WeiboAuth
      • Sharing: IWeiboAPI $\rightarrow$ IWeiboShareAPI
    • New Features:
      • Refactored authorization and sharing modules.
      • Open-sourced parts of the OpenAPI.
      • Added one-click login and login/logout controls.
      • Provided FAQ documentation.
  5. Quickstart guide for Weibo Android SDK

    master

    To integrate the Weibo Android SDK, follow these steps:

    1. Review Documentation: Read the ReadMe for an overview and the 微博Android平台SDK文档V2.4.0.pdf for deep technical details.
    2. Run Demo: Use the provided WeiboSDKDemo.apk or import the WeiboSDKDemo project to see all available features in action.
    3. Consult FAQ: If you encounter issues, check the [FAQ] first.
    4. API Reference: For specific class or API details, refer to WeiboSDK_API-V2.4.0.CHM.
  6. Configure authentication parameters in Constants

    master

    Before using the SDK, you must replace the placeholder values in the Constants interface with your own application credentials obtained from the Weibo Open Platform.

    Key parameters to configure:

    • APP_KEY: Your unique application key.
    • REDIRECT_URL: The callback page for authorization. For mobile clients, this is invisible to the user, but it must be defined for SDK authentication to work. The recommended default is https://api.weibo.com/oauth2/default.html.
    • SCOPE: A comma-separated string of OAuth2.0 permissions (e.g., email, direct_messages_read). You can manage and view available permissions in the Weibo Open Platform Management Center.
    public interface Constants {
        /** Replace with your own APP_KEY */
        public static final String APP_KEY      = "YOUR_APP_KEY";
    
        /** Recommended default callback page */
        public static final String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";
    
        /** Comma-separated list of requested scopes */
        public static final String SCOPE = "email,direct_messages_read,direct_messages_write";
    }
  7. Perform Authentication (SSO vs. Non-SSO)

    master

    The SDK supports two methods of authentication:

    1. Non-SSO Authentication

    Use this if you want to perform authentication without using the Weibo Single Sign-On (SSO) flow.

    mWeiboAuth.authorize(new AuthListener());

    2. SSO Authentication

    Use this to leverage the Weibo app's Single Sign-On capabilities. This requires an additional step in your Activity's onActivityResult.

    Step A: Initiate Authorization

    mSsoHandler = new SsoHandler(WBAuthActivity.this, mWeiboAuth);
    mSsoHandler.authorize(new AuthListener());

    Step B: Handle Callback in Activity You must override onActivityResult in the Activity that initiated the SSO login and call mSsoHandler.authorizeCallBack.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }
    // For SSO Authorization
    mSsoHandler = new SsoHandler(WBAuthActivity.this, mWeiboAuth);
    mSsoHandler.authorize(new AuthListener());
    
    // In your Activity's onActivityResult
    if (mSsoHandler != null) {
        mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
    }
  8. Prepare for Weibo SDK integration

    master

    Before integrating, complete these four steps:

    1. Obtain AppKey

    Register your application on the Weibo Open Platform to get an AppKey and define your RedirectURI.

    2. Register Package Name and Signature

    You must register your Android package name (from AndroidManifest.xml) and your application signature (MD5 value generated via official tools) on the Weibo Open Platform. Incorrect registration will cause authorization to fail.

    3. Choose Integration Method

    • Directly import weibosdkcore.jar: Best for projects only requiring authorization and sharing.
    • Reference WeiboSDK Library: Best for projects requiring login buttons and OpenAPI access.

    4. Add Required Permissions

    Add the following permissions to your AndroidManifest.xml:

    <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" />
  9. Implement WeiboAuthListener for authentication callbacks

    master

    To handle the results of an authentication attempt, implement the WeiboAuthListener interface.

    • onComplete(Bundle values): Triggered when authorization succeeds or fails with a specific error code. Use Oauth2AccessToken.parseAccessToken(values) to extract the token. If isSessionValid() is true, save the token using AccessTokenKeeper.writeAccessToken(). If it returns false, check for a code string in the bundle (often indicating an incorrect application signature).
    • onCancel(): Triggered when the user cancels the authorization.
    • onWeiboException(WeiboException e): Triggered when a Weibo-specific error occurs.
    class AuthDialogListener implements WeiboAuthListener {
        @Override
        public void onComplete(Bundle values) {
            // Parse Token from Bundle
            mAccessToken = Oauth2AccessToken.parseAccessToken(values);
            if (mAccessToken.isSessionValid()) {
                // Save Token to SharedPreferences
                AccessTokenKeeper.writeAccessToken(WBAuthActivity.this, mAccessToken);
            } else {
                // If signature is incorrect, a 'code' might be returned
                String code = values.getString("code", "");
            }
        }
    
        @Override
        public void onCancel() {}
    
        @Override
        public void onWeiboException(WeiboException e) {}
    }