public void Initialize() { _authorizer = new OAuthAuthorizer(Setting.GlobalConsumerKey.Value ?? App.ConsumerKey, Setting.GlobalConsumerSecret.Value ?? App.ConsumerSecret); CurrentAuthenticationStep = AuthenticationStep.RequestingToken; Observable.Defer(() => _authorizer.GetRequestToken(RequestTokenEndpoint)) .Retry(3, TimeSpan.FromSeconds(3)) // twitter sometimes returns an error without any troubles. .Subscribe(t => { _currentRequestToken = t.Token; CurrentAuthenticationStep = AuthenticationStep.WaitingPinInput; BrowserHelper.Open(_authorizer.BuildAuthorizeUrl(AuthorizationEndpoint, t.Token)); }, ex => this.Messenger.Raise(new TaskDialogMessage( new TaskDialogOptions { Title = "認証失敗", MainIcon = VistaTaskDialogIcon.Error, MainInstruction = "Twitterと正しく通信できませんでした。", Content = "何度も繰り返し発生する場合は、しばらく時間を置いて試してみてください。", CommonButtons = TaskDialogCommonButtons.Close, FooterIcon = VistaTaskDialogIcon.Information, FooterText = "コンピュータの時計が大幅にずれている場合も認証が行えないことがあります。" }))); }
private void Button_Click(object sender, RoutedEventArgs e) { LoginText = "Please wait..."; var authorizer = new OAuthAuthorizer(ConsumerKey, ConsumerSecret); authorizer.GetRequestToken(BaseUrl + "/oauth", new Codeplex.OAuth.Parameter("oauth_callback", "http://localhost/myapp")) .Select(res => res.Token) .ObserveOnDispatcher() .Subscribe(token => { requestToken = token; var url = authorizer.BuildAuthorizeUrl(BaseUrl + "/OAuth.action", token); wbLogin.Navigate(new Uri(url)); // navigate browser }); }
public static IObservable<AccessTokenData> GetAccessToken(this TwitterClient client, string consumerKey, string consumerSecret, RequestToken token, string pincode) { var authorizer = new OAuthAuthorizer(consumerKey, consumerSecret); if (client.CurrentNetworkProfile != null) { if (client.CurrentNetworkProfile.Proxy != null) { authorizer.ApplyBeforeRequest += req => req.Proxy = client.CurrentNetworkProfile.Proxy.GetProxy(); } } return Observable .Defer(() => authorizer.GetAccessToken(RestApi.OAuthEndpoints["oauth/access_token"].Url, token, pincode)) .OnErrorRetry(3) .Select(res => new AccessTokenData { Token = res.Token, ScreenName = res.ExtraData["screen_name"].FirstOrDefault(), UserId = Convert.ToInt64(res.ExtraData["user_id"].FirstOrDefault()), }); }
/// <summary> /// Open linkedin auth page /// </summary> private void li_connection() { // Get linkedin authorization url for app var authorizer = new OAuthAuthorizer(SWLinkedInSettings.ConsumerKey, SWLinkedInSettings.ConsumerSecret); authorizer.GetRequestToken(SWLinkedInSettings.RequestTokenUri) .Select(res => res.Token) .ObserveOnDispatcher() .Subscribe(token => { // Save request token this.li_requestToken = token; //linkedInRequestToken = token; string uri = authorizer.BuildAuthorizeUrl(SWLinkedInSettings.AuthorizeUri, token); authBrowser.Navigate(new Uri(uri)); }); }
public ViewModel() { _getRequestToken = new Lazy<Command>(() => new Command(_ => { var authorizer = new OAuthAuthorizer(ConsumerKey, ConsumerSecret); authorizer.GetRequestToken("http://twitter.com/oauth/request_token") .Select(res => res.Token) .ObserveOnDispatcher() .Subscribe(token => { requestToken = token; AuthorizeUrl = authorizer.BuildAuthorizeUrl("http://twitter.com/oauth/authorize", token); }, e => MessageBox.Show(e.ToString())); _getAccessToken.Value.IsCanExecute = true; }) ); _getAccessToken = new Lazy<Command>(() => new Command(_ => { new OAuthAuthorizer(ConsumerKey, ConsumerSecret).GetAccessToken("http://twitter.com/oauth/access_token", requestToken, PinCode) .ObserveOnDispatcher() .Subscribe(res => { UserId = res.ExtraData["user_id"].First(); ScreenName = res.ExtraData["screen_name"].First(); accessToken = res.Token; _startGetTimeline.Value.IsCanExecute = true; }, e => MessageBox.Show(e.ToString())); CanGetTimeline(this, new EventArgs()); _getAccessToken.Value.IsCanExecute = false; AuthorizeUrl = ""; }, false) ); _startGetTimeline = new Lazy<Command>(() => { return new Command(_ => { StreamingApi = new OAuthClient(ConsumerKey, ConsumerSecret, accessToken) { Url = "https://userstream.twitter.com/2/user.json" } .GetResponseLines() .Where(s => !string.IsNullOrWhiteSpace(s)) // filter invalid data .Select(s => DynamicJson.Parse(s)).Publish(); StreamingApi.Take(1).Subscribe(x => friendList = new HashSet<int>(((double[])x.friends).Select(id => (int)id)), e => MessageBox.Show(e.ToString(), "FriendList")); StreamingApi.Subscribe(x => this.PropertyChanged(x, new PropertyChangedEventArgs("StreamingApi")), e => MessageBox.Show(e.ToString(), "プロパティ変更")); StreamingApi .Where(x => x.text()) .ObserveOnDispatcher() .Subscribe(x => { _tweet.Add(new TimelineItemViewModel(x)); this.PropertyChanged(this, new PropertyChangedEventArgs("Tweet")); }); ((IConnectableObservable<dynamic>)StreamingApi).Connect(); _startGetTimeline.Value.IsCanExecute = false; }, false); }); //PropertyChanged += new PropertyChangedEventHandler((sender, target) => { if (target.PropertyName == "StreamingApi") MessageBox.Show(); }); CanGetTimeline += new EventHandler((sender, e) => _startGetTimeline.Value.IsCanExecute = true); }