public virtual Result Create( User user, String connectType, AccessToken token ) { UserConnect connect = GetConnectInfo( token.Uid, connectType ); if (connect != null) { return new Result( "本帐号已经绑定: uid=" + token.Uid + ",connectType=" + connectType ); } UserConnect x = new UserConnect(); x.User = user; x.ConnectType = connectType; x.Uid = token.Uid; x.Name = token.Name; x.AccessToken = token.Token; x.RefreshToken = token.RefreshToken; x.ExpiresIn = token.ExpiresIn; x.Scope = token.Scope; Result result = x.insert(); if (result.IsValid) { user.IsBind = 1; user.update(); } return result; }
private void loadUserProfile( AuthConnect connect, AccessToken accessToken ) { OAuthUserProfile user = connect.GetUserProfile( accessToken ); ctx.SetItem( "__currentOAuthUser", user ); ctx.SetItem( "__currentAccessToken", accessToken ); content( loadHtml( confirmUserInfo ) ); }
private AccessToken getAccessToken() { AccessToken x = new AccessToken(); String uid = ctx.Post( "uid" ); String accessToken = ctx.Post( "accessToken" ); String refreshToken = ctx.Post( "refreshToken" ); int expiresIn = ctx.PostInt( "expiresIn" ); String scope = ctx.Post( "scope" ); x.Token = accessToken; x.RefreshToken = refreshToken; x.Uid = uid; x.ExpiresIn = expiresIn; x.Scope = scope; return x; }
private void checkAccessToken( UserConnect x, AccessToken token ) { x.AccessToken = token.Token; x.RefreshToken = token.RefreshToken; x.ExpiresIn = token.ExpiresIn; x.Scope = token.Scope; // 重新获取access token之后,服务器会自动延续授权时间 x.Updated = DateTime.Now; x.update(); }
public void testProfile() { Console.WriteLine( "请按任意键,开始发布……" ); Console.ReadLine(); QQConnect connect = AuthConnectFactory.GetConnect( typeof( QQConnect ).FullName ) as QQConnect; if (connect == null) throw new Exception( "尚未配置" ); AccessToken x = new AccessToken(); x.Token = accessToken; OAuthUserProfile obj = connect.GetUserProfile( x ); Assert.IsNotNull( obj ); Console.WriteLine( "id=" + obj.Uid ); Console.WriteLine( "name=" + obj.Name ); Assert.IsNotNull( obj.Uid ); Assert.IsNotNull( obj.Name ); Assert.IsTrue( obj.Name.Length > 0 ); // 用户头像(小50×50像素) Console.WriteLine( "profile_image_url=" + obj.PicUrlSmall ); Assert.IsNotNull( obj.PicUrlSmall ); Assert.IsTrue( obj.PicUrlSmall.Length > 0 ); // 用户头像(大) Console.WriteLine( "avatar_large=" + obj.PicUrlBig ); Assert.IsNotNull( obj.PicUrlBig ); Assert.IsTrue( obj.PicUrlBig.Length > 0 ); Console.WriteLine( "domain=" + obj.FriendlyUrl ); Console.WriteLine( "请按 Enter 键结束……" ); Console.ReadLine(); }
public void testUploadPic() { QQConnect connect = AuthConnectFactory.GetConnect( typeof( QQConnect ).FullName ) as QQConnect; if (connect == null) throw new Exception( "尚未配置" ); AccessToken x = new AccessToken { Token = accessToken }; String content = "这是来自QQ互联api的微博内容" + DateTime.Now; JsonObject jsonValue = connect.PublishPic( x, content, "c:\\testpic.jpg" ); Assert.AreEqual( "ok", jsonValue.Get( "msg" ) ); }
// access_token=xxx&expires_in=888888&refresh_token=xxx&openid=xxx&name=xxx&nick=xxx&state= private static AccessToken parseAccessTokenQueryString( string response ) { String[] arr = response.Split( '&' ); AccessToken x = new AccessToken(); foreach (String item in arr) { String[] arrPair = item.Split( '=' ); if (arrPair.Length != 2) continue; if (arrPair[0] == "openid") { x.Uid = arrPair[1]; } else if (arrPair[0] == "name") { continue; // 有openId的场合 } else { x.SetValue( arrPair[0], arrPair[1] ); } } return x; }
private static AccessToken getAccessTokenByResponse( String response ) { if (strUtil.IsNullOrEmpty( response )) throw new NullReferenceException( "无法获取 access token,response为空" ); logger.Info( "parse access token: " + response ); response = response.Trim(); try { AccessToken accessToken; if (response.StartsWith( "{" ) == false && response.IndexOf( "&" ) > 0 && response.IndexOf( "=" ) > 0) { accessToken = parseAccessTokenQueryString( response ); } else { // {"access_token":"XXX","expires_in":157679999,"uid":"XXX"} accessToken = new AccessToken( response ); } if (strUtil.IsNullOrEmpty( accessToken.Token )) { throw new HttpClientException( "解析 access token,结果为空:" + response ); } return accessToken; } catch (Exception ex) { throw new HttpClientException( "解析 access token 失败" + Environment.NewLine + ex.Message ); } }
public abstract OAuthUserProfile GetUserProfile( AccessToken accessToken );
public virtual String GetUid( AccessToken accessToken ) { return accessToken.Uid; }
public void testProfile() { // 如果报错 source paramter(appkey) is missing,很有可能是 access token 过期 Console.WriteLine( "请按任意键,开始发布……" ); Console.ReadLine(); WeiboConnect connect = AuthConnectFactory.GetConnect( typeof( WeiboConnect ).FullName ) as WeiboConnect; if (connect == null) throw new Exception( "尚未配置" ); AccessToken x = new AccessToken(); x.Token = accessToken; x.Uid = "3214151865"; OAuthUserProfile obj = connect.GetUserProfile( x ); Console.WriteLine( "id=" + obj.Uid ); Console.WriteLine( "name=" + obj.Name ); // 用户头像(小50×50像素) Console.WriteLine( "profile_image_url=" + obj.PicUrlSmall ); // 用户头像(大) Console.WriteLine( "avatar_large=" + obj.PicUrlBig ); Console.WriteLine( "domain=" + obj.FriendlyUrl ); Console.WriteLine( "请按 Enter 键结束……" ); Console.ReadLine(); }
public void testUploadPic() { Console.WriteLine( "请按任意键开始……" ); Console.ReadLine(); QQWeiboConnect connect = AuthConnectFactory.GetConnect( typeof( QQWeiboConnect ).FullName ) as QQWeiboConnect; if (connect == null) throw new Exception( "尚未配置" ); AccessToken x = new AccessToken { Token = accessToken, Uid = openId }; JsonObject jsonValue = connect.PublishPic( x, "这是测试的微博内容" + DateTime.Now, "c:\\testpic.jpg" ); Assert.AreEqual( "ok", jsonValue["msg"].ToString() ); }
public void testPublish() { Console.WriteLine( "请按任意键,开始发布……" ); Console.ReadLine(); QQWeiboConnect connect = AuthConnectFactory.GetConnect( typeof( QQWeiboConnect ).FullName ) as QQWeiboConnect; if (connect == null) throw new Exception( "尚未配置" ); AccessToken x = new AccessToken { Token = accessToken, Uid = openId }; JsonObject jsonValue = connect.PublishPost( x, "这是测试的微博内容" + DateTime.Now ); Assert.AreEqual( "ok", jsonValue["msg"] ); }