public void Connect(int id) { WeiboType type = WeiboType.GetById(id); if (type == null) { echoRedirect("not found"); return; } switch (type.Name.ToLower()) { case "sina": SinaWeibo w = new SinaWeibo(type.AppKey, type.AppSecret); redirectUrl(w.GetAuthorizationUri(ctx.url.SiteUrl.TrimEnd('/') + to(SinaWeiboCallback))); break; case "qqweibo": processQQWeibo(type); break; case "qqspace": break; case "163": break; default: break; } }
void CreateDirectServices() { // create direct services to social networks _twitter = new Twitter(); _sinaWeibo = new SinaWeibo(); //TODO: init facebook with your app ID and an array of permissions _facebook = new Facebook(); _facebook.Init("1234567890", new string[] { "read_stream", "email", "publish_stream" }); }
public void Sync(IUser user, string text, string picUrl) { if (user == null) return; if (string.IsNullOrEmpty(text)||text.Length>140) return; IUserWeiboSettingService userWeiboSettingService = new UserWeiboSettingService(); WeiboType type = WeiboType.GetByName("sina"); SinaWeibo weibo = new SinaWeibo(type.AppKey, type.AppSecret); if (type == null || type.Enable!=1 ) return; UserWeiboSetting setting = userWeiboSettingService.Find(user.Id, type.Id); //用户未绑定微博或用户暂时选择同步 if (setting == null || setting.IsSync==0) return; //如果token过期而refreshToken不为空的话,重新得到token,这里主要是新浪oauth2.0api的refresh token需要通过新浪认证后才能得到 if (setting.IsExpire && !string.IsNullOrEmpty(setting.RefreshToken)) { try { SinaOAuthAccessToken token = weibo.RefreshAccessToken(setting.RefreshToken); setting.AccessToken = token.Token; setting.RefreshToken = token.RefreshToken; setting.ExpireIn = token.ExpiresIn; userWeiboSettingService.Update(setting); } catch (SinaWeiboException ex) { log.Error(ex.Message); } } if (!setting.IsExpire) { weibo.SetToken(setting.AccessToken); try { //发送消息 if (string.IsNullOrEmpty(picUrl)) { weibo.PostStatus(new UpdateStatusInfo() { Status = text }); } else { weibo.PostStatusWithPic(new UpdateStatusWithPicInfo { Status = text, Pic = picUrl }); } } catch (SinaWeiboException ex) { log.Error(ex.Message); } } }
void CreateDirectServices() { // create direct services to social networks _twitter = new Twitter(); _twitter.InitializationCompleted += OnDirectServiceInit; _twitter.InitializationFailed += OnDirectServiceInitFailed; _twitter.Init(); _sinaWeibo = new SinaWeibo(); _sinaWeibo.InitializationCompleted += OnDirectServiceInit; _sinaWeibo.InitializationFailed += OnDirectServiceInitFailed; _sinaWeibo.Init(); // init facebook with your app ID and an array of permissions _facebook = new Facebook(); _facebook.InitializationCompleted += OnDirectServiceInit; _facebook.InitializationFailed += OnDirectServiceInitFailed; _facebook.Init(facebookAppID, new string[] { "read_stream", "email", "publish_stream" }); }
/// <summary> /// 测试微博 /// </summary> public void TestWeibo() { SinaWeibo weibo = new SinaWeibo(); string path = "E:\\t"; List <string> messageList = new List <string>(); Console.WriteLine("======开始读取=========="); messageList = weibo.GetMessageList(path); if (messageList != null && messageList.Count > 0) { Console.WriteLine("Message Count = " + messageList.Count); } Console.WriteLine("======结束读取=========="); Console.WriteLine("======开始写文件=========="); string fileName = "E:\\weibo.txt"; weibo.OutputMessage(messageList, fileName); Console.WriteLine("======写文件成功=========="); Console.ReadKey(); }
private void processSina() { WeiboType type = WeiboType.GetByName("sina"); if (type == null) return; SinaWeibo w = new SinaWeibo(type.AppKey, type.AppSecret); //这里 因为oauth2.0验证 必须而且只能填写一个callback地址,所以这里直接转SinaWeiboCallback处理,处理后再返回来 redirectUrl(w.GetAuthorizationUri(ctx.url.SiteUrl.TrimEnd('/') + to(new UserWeiboSettingController().SinaWeiboCallback))); }
public void SinaWeiboCallback() { string code = ctx.Get("code"); if (string.IsNullOrEmpty(code)) { return; } WeiboType type = WeiboType.GetByName("sina"); if (type == null) { log.Error("找不到 sina 的 WeiboType,请添加"); return; } SinaWeibo w = new SinaWeibo(type.AppKey, type.AppSecret); Common.SinaOAuthAccessToken token = w.GetAccessTokenByAuthorizationCode(code, ctx.url.SiteUrl.TrimEnd('/') + to(SinaWeiboCallback)); UserWeiboSetting setting = _weiboService.Find(type.Id, token.Token,string.Empty); //如果用户已经微博绑定此帐户 if (setting != null) { //这里做用户登陆处理 User user = userService.GetById(setting.UserId); if (user != null) { loginService.Login(user, LoginTime.OneMonth, ctx.Ip, ctx); redirectUrl("/"); return; } else { echoRedirect("发生未知错误,请重试"); return; } } w.SetToken(token.Token); Data.Sina.User.UserInfo weiboUser = w.GetUserInfo(long.Parse(token.UserID)); if (weiboUser == null) { echoRedirect("很抱歉,获取失败,请重试", to(Index)); return; } //用户是未登陆,那就是用户通过微博注册或是通过此微博直接登陆 if (!ctx.viewer.IsLogin) { redirectSinaWeiboRegister(type, token,weiboUser.ScreenName,weiboUser.ProfileImageUrl); return; } //判断用户是否已经绑定了微博,没有绑定则添加,否则更新token setting = _weiboService.Find(ctx.viewer.Id, type.Id); Result result; if (setting == null) { setting = new UserWeiboSetting(); } setting.WeiboUid = token.UserID; setting.RefreshToken = token.RefreshToken; setting.WeiboType = type.Id; setting.WeiboName = type.Name; setting.IsSync = 1; setting.AccessToken = token.Token; setting.RefreshToken = token.RefreshToken; setting.ExpireIn = token.ExpiresIn; setting.BindTime = DateTime.Now; setting.UserId = ctx.viewer.Id; if (setting.Id==default(int)) result = _weiboService.Bind(setting); else result = _weiboService.Update(setting); if (result.HasErrors) { errors.Errors.AddRange(result.Errors); string error = string.Empty; result.Errors.ForEach(c => error = error + c + System.Environment.NewLine); log.Error(error); echoRedirect("很抱歉,绑定失败,请重试", to(Index)); } else { echoRedirect("绑定成功"); } }
public void OnSinaWeiboClick() { WeiboAuth.AddWeiboAuthListener(this); StartCoroutine(SinaWeibo.Bind()); }