### Initiate Weibo Authentication (Non-SSO) Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Call this method to start the Weibo authentication process if you are not using Single Sign-On (SSO). A new AuthListener instance should be provided. ```java mWeiboAuth.anthorize(new AuthListener()); ``` -------------------------------- ### Initiate Weibo Authentication (SSO) Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Use this method to start the Weibo authentication process with Single Sign-On (SSO) enabled. Ensure that the onActivityResult method in your Activity handles the SSO callback. ```java mSsoHandler = new SsoHandler(WBAuthActivity.this, mWeiboAuth); mSsoHandler.authorize(new AuthListener()); ``` -------------------------------- ### Initialize WeiboAuth Object Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Create a WeiboAuth object using your application's APP_KEY, REDIRECT_URL, and SCOPE. These parameters are essential for the authentication process. ```java mWeiboAuth = new WeiboAuth(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE); ``` -------------------------------- ### Configure Application Constants for Weibo SDK Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Replace placeholder values in the Constants interface with your application's specific APP_KEY, REDIRECT_URL, and SCOPE. Ensure your signature is correctly registered on the Weibo platform. ```java public interface Constants { /** 当前 DEMO 应用的 APP_KEY,第三方应用应该使用自己的 APP_KEY 替换该 APP_KEY */ public static final String APP_KEY = "2045436852"; /** * 当前 DEMO 应用的回调页,第三方应用可以使用自己的回调页。 * *
* 注:关于授权回调页对移动客户端应用来说对用户是不可见的,所以定义为何种形式都将不影响, * 但是没有定义将无法使用 SDK 认证登录。 * 建议使用默认回调页:https://api.weibo.com/oauth2/default.html *
*/ public static final String REDIRECT_URL = "http://www.sina.com"; /** * Scope 是 OAuth2.0 授权机制中 authorize 接口的一个参数。通过 Scope,平台将开放更多的 * 微博核心功能给开发者,同时也加强用户隐私保护,提升了用户体验,用户在新 OAuth2.0 授权 * 页中有权利选择赋予应用的功能。 * 我们通过新浪微博开放平台-->管理中心-->我的应用-->接口管理处,能看到我们目前已有哪些接 * 口的使用权限,高级权限需要进行申请。 * 目前 Scope 支持传入多个 Scope 权限,用逗号分隔。 * * 有关哪些 OpenAPI 需要权限申请,请查看:http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI * 关于 Scope 概念及注意事项,请查看:http://open.weibo.com/wiki/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"; } ``` -------------------------------- ### Implement WeiboAuthListener for Authentication Callback Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Implement the WeiboAuthListener interface to handle the results of the authentication process. This includes parsing access tokens, saving them, and handling errors or cancellations. ```java /** * 微博认证授权回调类。 * 1.SSO 授权时,需要在 {@link #onActivityResult} 中调用 {@link SsoHandler#authorizeCallBack} * 后,该回调才会被执行。 * 2. 非 SSO 授权时,当授权结束后,该回调就会被执行。 * 当授权成功后,请保存该 access_token、expires_in、uid 等信息到 SharedPreferences 中。 */ class AuthDialogListener implements WeiboAuthListener { @Override public void onComplete(Bundle values) { // 从 Bundle 中解析 Token mAccessToken = Oauth2AccessToken.parseAccessToken(values); if (mAccessToken.isSessionValid()) { // 保存 Token 到 SharedPreferences AccessTokenKeeper.writeAccessToken(WBAuthActivity.this, mAccessToken); ......... } else { // 当您注册的应用程序签名不正确时,就会收到 Code,请确保签名正确 String code = values.getString("code", ""); ......... } } @Override public void onCancel() { } @Override public void onWeiboException(WeiboException e) { } } ``` -------------------------------- ### Declare Necessary Permissions in AndroidManifest.xml Source: https://github.com/mobileresearch/weibo_android_sdk/blob/master/README.md Add these permissions to your AndroidManifest.xml file to enable internet access, network state, and external storage usage for the SDK. ```xml