Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the encapsulation of NET class library

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes "how to understand NET class library encapsulation". The content is detailed and easy to understand, the "NET class library encapsulation how to understand" interested friends can follow the editor's ideas slowly in-depth to read, I hope that after reading can be helpful to everyone. Let's learn more about "how to understand NET class library encapsulation" with the editor.

Currently open OAuth3 interface is more commonly used Sina Weibo, Tencent QQ, Taobao, in fact, there are Wechat, but I did not apply for Wechat, because the formalities are troublesome, seems to need to scan its agreement signature and then send it, so give up the encapsulation of Wechat OAuth3 landing interface, to be made up later! About Taobao OAuth website access to the current Taobao is suspended review seems, perhaps because deep-pocketed more capricious bar, but you can still apply for an account, you can do an application to Taobao.

one. The addresses of each OAuth3.0 application for cooperation are given below:

-- sinaweibo OAuth3.0--

Apply for access to OAuth3 cooperation address: http://open.weibo.com (Weibo. Open platform)

Authorized Url: https://api.weibo.com/oauth3/authorize?client_id= your ClientId&redirect_uri=, your callback Url&response_type=code&display=default%20&state=sinaweibo

-- qq OAuth3.0--

Apply for access to OAuth3 cooperation address: http://connect.qq.com (the so-called QQ interconnection, there seems to be a http://open.qq.com, but I haven't used this yet, just use QQ to connect first, but you can log in)

Authorized Url: http://openapi.qzone.qq.com/oauth/show?which=Login&display=pc&client_id= your ClientId&redirect_uri=, your callback Url&response_type=code&display=default%20&state=qq

-- taobao OAuth3.0--

Apply for access to OAuth3 Cooperation address: http://open.taobao.com/index.htm

Authorized Url: https://oauth.taobao.com/authorize?client_id= your ClientId&redirect_uri=, your callback Url&response_type=code&display=default%20&state=taobao

-- WeiXin OAuth3.0--

Apply for access to OAuth3 Cooperation address: https://open.weixin.qq.com

Next, we will start to give benefits to our friends on the dotNET platform, encapsulating a class that implements access to Sina Weibo, Tencent QQ, Taobao, and obtaining user information, and implements the following two interfaces, namely IOAuthClient and IUserInterface.

Namespace GeRenXing.OpenPlatform {public interface IOAuthClient {AuthOption Option {get;} AuthToken Token {get;} IUserInterface User {get;} String GetAuthorizeUrl (ResponseType responseType); AuthToken GetAccessTokenByAuthorizationCode (string code); AuthToken GetAccessTokenByPassword (string passport, string password); AuthToken GetAccessTokenByRefreshToken (string refreshToken); String Get (String url, params RequestOption [] options); String Post (String url, params RequestOption [] options) }}

If you need to encapsulate more api, you can expand it by yourself, or you can call the Get and Post methods of IOAuthClient directly to pass parameters to access api quickly.

Sina Weibo and Taobao return the OpenId directly to you when they obtain Token, while Tencent QQ does not return OpenId when it accesses OAuth3.0 to obtain Token. You need to visit https://graph.qq.com/oauth3.0/me separately to obtain OpenId. I have automatically dealt with this difference in the encapsulated Tencent QQ ParseAccessToken method, that is, I have initiated another request to obtain user OpenId.

Attention, everyone: the code returned by the authorized Url can only be used once, otherwise the following error will be reported:

{"error": "invalid_grant", "error_code": 21325, "request": "/ oauth3/access_token", "error_uri": "/ oauth3/access_token", "error_description": "invalid authorization code:2c2cb4e1f6b70650acbe1dad757ea6bb"}

two. The following has done a test console program, ClientId and ClientSecret,CallbackUrl please change to their own.

Using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using GeRenXing.OpenPlatform; namespace GeRenXing.OpenPlatform.Test {class Program {private static Dictionary MirroroauthClients; static void Main (string [] args) {/ / initialize the open platform client (replace with your own ClientId,ClientScrert,CallbackUrl) m_oauthClients = new Dictionary () M_oauthClients ["sinaweibo"] = new OpenPlatform.OAuthClient.SinaWeiBoClient ("You ClientId", "You ClientScrert", "You Callback Url"); m_oauthClients ["qq"] = new OpenPlatform.OAuthClient.TencentQQClient ("You ClientId", "You ClientScrert", "You Callback Url"); m_oauthClients ["taobao"] = new OpenPlatform.OAuthClient.TaoBaoClient ("You ClientId", "You ClientScrert", "You Callback Url") / / Test OAuthTest ("sinaweibo"); / / OAuthTest ("qq"); / / OAuthTest ("taobao"); Console.ReadKey (true);} private static void OAuthTest (String platformCode) {String authorizeUrl = String.Empty; if (String.IsNullOrEmpty (platformCode)) platformCode = "sinaweibo" Console.WriteLine ("OpenPlatform Request For" + platformCode); Console.WriteLine ("); IOAuthClient oauthClient = m _ oauthClients [platform Code]; oauthClient.Option.State = platformCode; / / * step: obtain the open platform authorization address authorizeUrl = m _ oauthClients [platformCode] .GetAuthorizeUrl (ResponseType.Code) Console.WriteLine ("Step 1-OAuth3.0 for Redirect AuthorizeUrl:"); Console.WriteLine (authorizeUrl); / / second step: open an IE browser to get Code Process p = new Process (); ProcessStartInfo psi = new ProcessStartInfo (); psi.Arguments = authorizeUrl; psi.FileName = "C:\ Program Files\\ Internet Explorer\\ iexplore.exe" P.StartInfo = psi; p.Start (); Console.WriteLine ("); Console.WriteLine (" OAuth3.0 Input Server Response Code "); String code = Console.ReadLine (); / / step 3: obtain the open platform authorization token oauthClient = m _ oauthClients [platforms Code]; AuthToken accessToken = oauthClient.GetAccessTokenByAuthorizationCode (code) If (accessToken! = null) {Console.WriteLine (""); Console.WriteLine ("Step 2-OAuth3.0 for AccessToken:" + accessToken.AccessToken); / / output raw response data Console.WriteLine ("GetAccessToken Raw Response:"); Console.WriteLine (oauthClient.Token.TraceInfo) / / step 4: call the open platform API to get the open platform user information dynamic oauthProfile = oauthClient.User.GetUserInfo (); / / output the parsed user nickname Console.WriteLine (""); Console.WriteLine ("Step 3-Call Open API UserInfo:") Console.WriteLine ("UserInfo Nickname:" + oauthClient.Token.User.Nickname); / / output raw response data Console.WriteLine ("GetUserInfo Raw Response:"); Console.WriteLine (oauthClient.Token.TraceInfo);}

three. Here are some screenshots of the test:

SianWeiBo Oauth3.0

+ + +

+ + +

QQ OAuth3.0

+ + +

+ + +

TaoBao OAuth3.0

+ + +

On how to understand the NET class library encapsulation to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report