首页 > 代码库 > 用于新浪微博,腾讯QQ,淘宝 OAuth2.0 登陆的NET类库封装
用于新浪微博,腾讯QQ,淘宝 OAuth2.0 登陆的NET类库封装
目前开放OAuth2互联接口比较常用的有新浪微博,腾讯QQ,淘宝,其实还有微信,只是微信的我木有申请下来,因为手续麻烦,貌似需要扫描它的协议签字然后发过去,所以就放弃封装微信OAuth2登陆接口了,待以后给补上!关于淘宝OAuth网站接入登陆目前淘宝是暂停审核了貌似,也许是因为财大气粗比较任性吧,不过你还是可申请账号的,可以做应用提交给淘宝。
需要源代码的小伙伴请猛戳这里下载,需要已经编译好Dll的小伙伴请猛戳这里下载。
一。 下面给出各个OAuth2.0申请合作的地址:
-- sinaweibo OAuth2.0 --
申请接入OAuth2合作地址:http://open.weibo.com (微博.开放平台)
授权Url: https://api.weibo.com/oauth2/authorize?client_id=你的ClientId&redirect_uri=你的回调Url&response_type=code&display=default%20&state=sinaweibo
-- qq OAuth2.0 --
申请接入OAuth2合作地址: http://connect.qq.com (所谓的QQ互联,貌似还有一个http://open.qq.com,但这个我还木有用过,就先用QQ互联吧反正是可以登陆)
授权Url: http://openapi.qzone.qq.com/oauth/show?which=Login&display=pc&client_id=你的ClientId&redirect_uri=你的回调Url&response_type=code&display=default%20&state=qq
-- taobao OAuth2.0 --
申请接入OAuth2合作地址:http://open.taobao.com/index.htm
授权Url: https://oauth.taobao.com/authorize?client_id=你的ClientId&redirect_uri=你的回调Url&response_type=code&display=default%20&state=taobao
-- WeiXin OAuth2.0 --
申请接入OAuth2合作地址:https://open.weixin.qq.com
下面开始给dotNET平台的小伙伴们发福利啦,封装了一个类实现了接入到新浪微博,腾讯QQ,淘宝,和获取用户信息,就实现了下面的2个接口,分别是IOAuthClient和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); }}
namespace GeRenXing.OpenPlatform{ public interface IUserInterface { dynamic GetUserInfo(); void EndSession(); }}
需要封装更多api的请自行扩展,也可以直接调用IOAuthClient的Get和Post方法直接传递参数快速访问api。
新浪微博和淘宝在获取Token的时候就把OpenId直接返回给你了,而腾讯QQ的OAuth2.0接入里获取Token是不会返回OpenId的,需要单独访问https://graph.qq.com/oauth2.0/me获取OpenId,对于这个不同点我在封装的腾讯QQ ParseAccessToken 方法里面已经自动处理了,就是再次发起了一次获取用户Open Id的请求。
各位小伙伴注意啦:访问授权Url返回的Code码只能使用一次,否则会报下面这个错误:
{"error":"invalid_grant","error_code":21325,"request":"/oauth2/access_token","error_uri":"/oauth2/access_token","error_description":"invalid authorization code:2c2cb4e1f6b70650acbe1dad757ea6bb"}
二。 下面做了个测试的控制台程序,ClientId和ClientSecret,CallbackUrl请改为自己的。
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<String, IOAuthClient> m_oauthClients; static void Main(string[] args) { //初始化开放平台客户端(请替换成自己的ClientId,ClientScrert,CallbackUrl) m_oauthClients = new Dictionary<string, IOAuthClient>(); 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"); //测试 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[platformCode]; oauthClient.Option.State = platformCode; //第一步:获取开放平台授权地址 authorizeUrl = m_oauthClients[platformCode].GetAuthorizeUrl(ResponseType.Code); Console.WriteLine("Step 1 - OAuth2.0 for Redirect AuthorizeUrl: "); Console.WriteLine(authorizeUrl); //第二步:打开IE浏览器获取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("OAuth2.0 Input Server Response Code"); String code = Console.ReadLine(); //第三步:获取开放平台授权令牌 oauthClient = m_oauthClients[platformCode]; AuthToken accessToken = oauthClient.GetAccessTokenByAuthorizationCode(code); if (accessToken != null) { Console.WriteLine(""); Console.WriteLine("Step 2 - OAuth2.0 for AccessToken: " + accessToken.AccessToken); //输出原始响应数据 Console.WriteLine("GetAccessToken Raw Response : "); Console.WriteLine(oauthClient.Token.TraceInfo); //第四步:调用开放平台API,获取开放平台用户信息 dynamic oauthProfile = oauthClient.User.GetUserInfo(); //输出解析出来的用户昵称 Console.WriteLine(""); Console.WriteLine("Step 3 - Call Open API UserInfo: "); Console.WriteLine("UserInfo Nickname: " + oauthClient.Token.User.Nickname); //输出原始响应数据 Console.WriteLine("GetUserInfo Raw Response : "); Console.WriteLine(oauthClient.Token.TraceInfo); } } }}
三。 下面给出几张测试的截图:
SianWeiBo Oauth2.0
++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++
QQ OAuth2.0
++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++
TaoBao OAuth2.0
++++++++++++++++++++++++++++++++++++++++++++++++++++
用于新浪微博,腾讯QQ,淘宝 OAuth2.0 登陆的NET类库封装