Sina Weibo SDK for. Net 4.0 version 2 was officially released.

Source: Internet
Author: User
Tags oauth

You can go here to view the features and usage of the first version.

Note the following when downloading the demo:There are too many friends who downloaded and tested the demo in the past few days. The appkey provided in the demo has not been submitted for review, so there is a maximum number of authorized users. Please change the appkey, appsecret, and callback address on your own, you can change it to your own. Please refer to http://weibosdk.codeplex.com/wikipage? Title = How to bind and modify the callback address in the Sina background

First of all, I would like to thank you for your comments and suggestions. With your support, you will be more motivated to start things. ^_^

Some new features of the second SDK

    • Oauth2.0, which is officially promoted by Sina, is used for authorization and authentication.
    • All interfaces have been upgraded to the official V2 API, that is, they can be operated using the official V2 API model.
    • Referring to the official php sdk, the authorization authentication method and API operation method are separated into two classes, which are logically clearer.

 

Usage

Step 1: Authorization

Traditional Method (applicable to web projects)

    • Initialize oauth class
    • Get authorize address
    • Access the authorize address and perform authorization to obtain the code
    • Use the getaccesstokenbyauthorizationcode method of the oauth class to obtain the accesstoken
    • Complete

Simulate Logon (applicable to winform projects and other projects)

    • Initialize oauth class
    • Call the clientlogin method in the oauth class, pass in the account, password, and bind the callback address (the clientlogin method simulates the entire authorization process and implements a login and authorization)
    • Complete

Step 2: instantiate the operation class

So far, authorization is complete. Next, we use the oauth as the parameter to instantiate a client operation class.

After the client is instantiated, various methods can be called through the API namespace.

 

Simple Use Cases

Based on the normal authorization authentication process

 // Initialize oauth and prepare for authentication  VaR Oauth = New Netdimension. Weibo. oauth ( "1028898141" , "78be07c9bcfa30b7871788d3778ce131" ); /** Normal process or Web process: * 1. obtain the authorization address * 2. access Authorization address * 3. after successful authorization, the system automatically redirects to the website specified by callback and obtains Code * 4. exchange Code for access token */  VaR Url = oauth. getauthorizeurl ( Https://api.weibo.com/oauth2/default.html" , Responsetype. Code ); // Obtain the authorization address based on the authorization method. System. Diagnostics. process. Start (URL ); // Simulate a pop-up window. The browser is opened directly in the Console mode. You can use IFRAME and new window to open the page as needed.  // Open the browser and perform the authorization process. Then, the URL specified by callback is redirected and the code is obtained.  // Enter the code you just obtained Console. Write ( "Enter the code parameter in the browser address :" ); VaR Code = console. Readline (); // Obtain the accesstoken according to the code  VaR Accesstoken = oauth. getaccesstokenbyauthorizationcode (code, Https://api.weibo.com/oauth2/default.html" ); // Note: the specified URL of callback must be consistent.  // Check the access token. Console. writeline (accesstoken ); // Now, we have obtained the accesstoken 

Use the clientlogin method after the simplified process

// Initialize oauth and prepare for authenticationVaROauth =NewNetdimension. Weibo. oauth ("1028898141","78be07c9bcfa30b7871788d3778ce131");// Simplify the authentication process and directly call clientlogin. This method does not need to apply for password authentication, but simulates the above steps and encapsulatesVaRResult = oauth. clientlogin ("<Your Weibo account>","<Your Weibo address>",Https://api.weibo.com/oauth2/default.html");// The return value is bool type. If it is true, authorization and logon are successful. If the value is false, the password is incorrect, or the callback address is different from the one entered in the Sina background.Console. writeline (oauth. accesstoken );// Check the accesstoken obtained here. In theory, it should be the same as the token obtained by the above method.

Interface call example

After obtaining the accesstoken in the preceding step, the oauth object is directly used as a parameter to instantiate an operating client instance. Things that follow, you know ^_^

// Instantiate an operation class and use the oauth instance that just obtained the accesstokenNetdimension. Weibo. Client Sina =NewNetdimension. Weibo. Client (oauth );// Call various methodsConsole. writeline (sina. API. statuses. friendstimeline ());// Get the latest WeiboConsole. writeline (sina. API. statuses. Update ("Publish a microblog to test the firepower! "+ Datetime. Now. tolongtimestring ()));// An exception occurs when no subsequent time is added to the test of a clockwork microblog. You cannot send Weibo messages repeatedly.// An example of getting dataVaRMentions = sina. API. statuses. mentions ();// Obtain data through the "mention my Weibo" Interface// Go To The http://open.weibo.com/wiki/2/statuses/mentions to check the mentions data structure, then let's operate the data.Foreach(VaRStatusInMentions. statuses ){If(Status. isdefined ("User"))// Check whether there is a user. If weibo is deleted, no user will be returned. If XXX. User is directly deleted, an error will occur.{Console. writeline (String. Format ("{0} said: {1 }", Status. User. screen_name, status. Text ));// Print the user name and content}}

 

Class and Method

The returned values of related methods and methods in the SDK are the same as those of the official API. Therefore, this part of content is not listed. For details, refer to the official API documentation.

Oauth class, which encapsulates authorization, authentication, logon, and other operation methods.

Oauth class
Oauth Constructor
Getauthorize Obtain the application authorization address and set the Return Method
Getaccesstokenbyauthorizationcode Obtain accesstoken using code
Getaccesstokenbyauthorizationcode To obtain the accesstoken Using password, you must apply for the permission officially.
Getaccesstokenbyrefreshtoken You can use the token method to obtain the accesstoken. You also need to apply for the permission to obtain the refreshtoekn.
Clientlogin Client Logon: simulates code authorization to complete Authorization and logon (commonly known as one-click logon)
Verifieraccesstoken Verify the validity of accesstoken

  

The client class encapsulates the Weibo operation methods in the official API. All methods are encapsulated in the API namespace by category, and the methods are classified by official documentation. For more information about parameters and return values, see the official documentation.

Client class
Client Constructor
API Encapsulation of all official interface methods. Methods have been encapsulated according to the categories in the official documentation, and the method name is basically consistent with the official API. The returned value is JSON, and dynamicjson is used for dynamic usage. You can directly obtain the content in the form of XXX. For the data structure of the returned content, see the official documentation.

 

My microblog: http://weibo.com/xuanchenlin

Project hosting address: http://weibosdk.codeplex.com

Welcome to the discussion!

 

The console demo only provides you with a way to quickly understand This SDK. Currently, the winform demo (download) has been released, web, WPF, and Windows Phone demos will be released later. Coming soon ~

Thank you for your support. Thank you ~

 

Join the QQ Group for discussion and study.

GROUP: 241088256

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.