Weibo Authorized Sharing and blog Authorized Sharing

Source: Internet
Author: User

Weibo Authorized Sharing and blog Authorized Sharing
1. Register a developer account

Enter the open platform provided by Sina Weibo registered Sina account: http://open.weibo.com/

You need to create an application in the management center to obtain the Appkey. If you only implement the sharing function, you do not need to submit the application for review.



2. Application Information

Basic information page: Android signature required

Signing tool: https://github.com/mobileresearch/weibo_android_sdk/blob/master/app_signatures.apk

Install it on your mobile phone. Open the signature tool and enter the package name to generate a unique MD5 verification code. enter it.


Advanced information page: Enter the authorization callback page, which must be consistent with the REDIRECT_URL in the program code, as shown in



3. Introduce WeiboSDK into your project WeiboSDK: http://open.weibo.com/wiki/SDK#Android_SDK


4. implement authentication and sharing
<Pre name = "code" class = "java">/*** this class demonstrates how third-party applications share content through Weibo clients. * Execution Process: choose this application> Weibo> This application */public class wb1_activity extends Activity implements Response, OnClickListener {/** Weibo share Button */private Button mShareButton; /** Weibo instance */private AuthInfo mAuthInfo;/** Note: SsoHandler is valid only when SDK supports SSO */private SsoHandler mSsoHandler;/** encapsulates "access_token ", "expires_in", "refresh_token", and provides their management functions */private oauth2accesen en mAccessToken;/** whether to install Weibo */private boolean isInstalled Weibo;/** support Api level */private int supportApiLevel;/** Weibo sharing interface instance */private iweibow.api mweibow.api; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_wbshare); // click mShareButton = (Button) findViewById (R. id. register_app_to_weibo); mShareButton. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// create a Weibo instance MAuthInfo = new AuthInfo (this, Constants. WB_APP_KEY, Constants. REDIRECT_URL, Constants. SCOPE); mSsoHandler = new SsoHandler (this, mAuthInfo); // SSO authorization, only the client mSsoHandler. authorizeClientSso (new AuthListener (); // create a Weibo SDK interface instance mweiboappsapi = WeiboShareSDK. createWeiboAPI (this, Constants. WB_APP_KEY); // register a third-party application to the Weibo client. After successful registration, the application is displayed in the Weibo Application List. // However, you need to apply for the permission to integrate and share the content in the attachment column. For details, see the Demo prompt. // NOTE: Be sure to register the content in advance, that is, when the interface is initialized or when the application is initialized, register mweibow.api. registerApp (); // when the Activity is reinitialized (when the Activity is in the background, it may be killed due to insufficient memory ), // call {@ link iweibow.api # handleweibo oresponse} to receive the data returned by the Weibo client. // If the execution succeeds, true is returned and {@ link IWeiboHandler is called. response # onResponse}; // if the call fails, false is returned. The above callback mweibow.api is not called. handleweibo oresponse (getIntent (), this); // obtain whether the Weibo client has the isInstalledWeibo = mweibow.api installed. isWeiboAppInstalled (); // supported SDK version supportApiLevel = mweibow.api. getWeiboAppSupportAPI ();}/*** callback class for Weibo Authentication Authorization. 1. For SSO authorization, the callback will be executed only after * {@ link SsoHandler # authorizeCallBack} is called in {@ link # onActivityResult. 2. When the authorization is not SSO *, the callback will be executed after the authorization is complete. After the authorization is successful, save the access_token, expires_in, uid, and other information to * SharedPreferences. */Class AuthListener implements WeiboAuthListener {@ Overridepublic void onComplete (Bundle values) {// parse TokenmAccessToken = Oauth2AccessToken from Bundle. parseAccessToken (values); // determines whether the AccessToken is valid if (mAccessToken. isSessionValid () {// Save the Token to SharedPreferencesAccessTokenKeeper. writeAccessToken (wb1_activity. this, mAccessToken); Toast. makeText (wb1_activity. this, "token obtained successfully", Toast. LENGTH_SHO RT ). show (); shareToWeiBo O ();} else {System. out. println ("authentication failed") ;}@ Overridepublic void onCancel () {Toast. makeText (wb1_activity. this, "cancel", Toast. LENGTH_LONG ). show () ;}@ Overridepublic void onWeiboException (WeiboException e) {Toast. makeText (wb1_activity. this, "Auth exception:" + e. getMessage (), Toast. LENGTH_LONG ). show () ;}/ *** share to Weibo */private void shareToWeiBo () {if (isInstalledWeibo) {if (mWeib OShareAPI. isWeiboAppSupportAPI () {if (supportApiLevel> = 10351/* ApiUtils. BUILD_INT_VER_2_2 */) {// 1. initialize the Weibo shared message WeiboMultiMessage weiboMessage = new WeiboMultiMessage (); // The content of shared text can also be shared with pictures, videos, and music. For details, see WeiboSDKDemoTextObject textObject = new TextObject (); textObject. text = "share message"; weiboMessage. textObject = textObject; // 2. initialize a message request from a third party to Weibo: sendmultimessagetoweibo orequest request = new SendMulti Messagetoweibo orequest (); // uniquely identifies a request with transaction. transaction = String. valueOf (System. currentTimeMillis (); request. multiMessage = weiboMessage; // 3. send a request message to Weibo, which calls the mweibow.api on the Weibo sharing interface. sendRequest (wb1_activity. this, request) ;}} else {Toast. makeText (this, "SDK not supported", Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (this, "Sina Weibo client not installed", Toast. LENGTH_LONG ). show () ;}/ *** @ see {@ link Activity # onN EwIntent} */@ Overrideprotected void onNewIntent (Intent intent) {super. onNewIntent (intent); // call this function to receive the data returned by the Weibo client from the current application after it is recalled and shared; if the execution is successful, return true and call // {@ link IWeiboHandler. response # onResponse}; if the callback fails, false is returned. The above callback mweibow.api is not called. handleweibo oresponse (intent, this);}/*** receives data from the micro-client. * This method is called when the Weibo client calls the current application and shares it. ** @ Param baseRequest: Weibo request data object * @ see {@ link iweibow.api # handleweibo orequest} */@ Overridepublic void onResponse (BaseResponse baseResp) {switch (baseResp. errCode) {case WBConstants. errorCode. ERR_ OK: Toast. makeText (this, "success", Toast. LENGTH_LONG ). show (); break; case WBConstants. errorCode. ERR_CANCEL: Toast. makeText (this, "cancel", Toast. LENGTH_LONG ). show (); break; case WBConstants. errorCode. ERR_FAIL: Toast. ma KeText (this, "failed" + baseResp. errMsg, Toast. LENGTH_LONG ). show (); break;}/*** this function is called when the SSO authorization Activity exits. ** @ See {@ link Activity # onActivityResult} */@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // SSO authorization callback // important: onActivityResultif (mSsoHandler! = Null) {mSsoHandler. authorizeCallBack (requestCode, resultCode, data );}}}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.