Пример #1
0
        void UpdateResponse(TwitterAsyncResponse <TwitterStatus> ee)
        {
            if (ee.ResponseObject == null)
            {
                TweetFailure(ee.Content);
                return;
            }

            InsertTweetIn(ee.ResponseObject, ColumnType.Timeline);
            if (ee.ResponseObject.Text.Contains("@" + Global.ThisUser.ScreenName))
            {
                InsertTweetIn(ee.ResponseObject, ColumnType.Mentions);
            }

            this.Invoke(new Action(delegate
            {
                textTweet.Enabled = true;
                textTweet.Text    = "";
                textTweet.Focus();

                buttonUpdate.Enabled = true;

                checkMedia.Enabled = true;
                checkMedia.Checked = false;
            }));
        }
Пример #2
0
        public void BeginAuthorize_Returns_If_Already_Authorized()
        {
            var oAuthMock = new Mock <IOAuthTwitter>();
            TwitterAsyncResponse <object> twitterResp = null;
            var pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = "consumerkey",
                    ConsumerSecret = "consumersecret",
                    OAuthToken     = "oauthtoken",
                    AccessToken    = "accesstoken"
                },
                OAuthTwitter             = oAuthMock.Object,
                GetPin                   = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };

            pinAuth.BeginAuthorize(resp => twitterResp = resp);

            oAuthMock.Verify(oauth =>
                             oauth.GetRequestTokenAsync(It.IsAny <Uri>(), It.IsAny <Uri>(), "oob", AuthAccessType.NoChange, false, It.IsAny <Action <string> >(), It.IsAny <Action <TwitterAsyncResponse <object> > >()),
                             Times.Never());
            Assert.Null(twitterResp);
        }
Пример #3
0
        public void ProcessDMCallback(TwitterAsyncResponse <TwitterDirectMessageCollection> ee)
        {
            TwitterDirectMessageCollection dms = ee.ResponseObject;

            if (dms == null)
            {
                return;
            }
            for (int i = dms.Count - 1; i >= 0; i--)
            {
                InsertDM(flowColumn, dms[i]);
            }
        }
Пример #4
0
        public void CompleteAuthorize_Invokes_AuthorizationCompleteCallback()
        {
            var pinAuth = new PinAuthorizer
            {
                Credentials              = new InMemoryCredentials(),
                OAuthTwitter             = new OAuthTwitterMock(),
                GoToTwitterAuthorization = link => { }
            };
            TwitterAsyncResponse <UserIdentifier> twitterResp = null;

            pinAuth.CompleteAuthorize("1234567", resp => twitterResp = resp);

            Assert.NotNull(twitterResp);
        }
Пример #5
0
        public void ProcessTweetsCallback(TwitterAsyncResponse <TwitterStatusCollection> ee)
        {
            TwitterStatusCollection tweets = ee.ResponseObject;

            if (tweets == null)
            {
                Global.DeckForm.TweetFailure(ee.Content);
                return;
            }
            for (int i = tweets.Count - 1; i >= 0; i--)
            {
                InsertTweet(flowColumn, tweets[i]);
            }
        }
Пример #6
0
        public void BeginAuthorize_Invokes_AuthorizationCompleteCallback()
        {
            var pinAuth = new PinAuthorizer
            {
                Credentials              = new InMemoryCredentials(),
                OAuthTwitter             = new OAuthTwitterMock(),
                GetPin                   = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };
            TwitterAsyncResponse <object> twitterResp = null;

            pinAuth.BeginAuthorize(resp => twitterResp = resp);

            Assert.NotNull(twitterResp);
        }
 private void SaveAuthInfo(TwitterAsyncResponse<UserIdentifier> completeResp)
 {
     auth.UserId = completeResp.State.UserID;
     auth.ScreenName = completeResp.State.ScreenName;
     SuspensionManager.SessionState["Authorizer"] = auth;
     LocalDataCredentials cred = new LocalDataCredentials
     {
         AccessToken = auth.Credentials.AccessToken,
         ConsumerKey = auth.Credentials.ConsumerKey,
         ConsumerSecret = auth.Credentials.ConsumerSecret,
         OAuthToken = auth.Credentials.OAuthToken,
         ScreenName = completeResp.State.ScreenName,
         UserId = completeResp.State.UserID
     };
     cred.Save();
     SuspensionManager.SessionState["SavedAuthorizer"] = auth;
 }
Пример #8
0
        /// <summary>
        /// Posts asynchronously to Twitter for access token
        /// </summary>
        /// <param name="accessTokenUrl">Access token URL</param>
        /// <param name="postData">Post info</param>
        /// <param name="authorizationCompleteCallback">Invoked when request finishes</param>
        public void PostAccessTokenAsync(Request request, IDictionary<string, string> postData, Action<TwitterAsyncResponse<UserIdentifier>> authenticationCompleteCallback)
        {
            var accessTokenUrl = new Uri(request.FullUrl);
            var req = GetHttpPostRequest(accessTokenUrl);

            req.ContentType = "application/x-www-form-urlencoded";

            var postBody = GatherPostData(postData);
            byte[] postDataBytes = Encoding.UTF8.GetBytes(postBody);

#if SILVERLIGHT
                req.BeginGetRequestStream(
                    new AsyncCallback(
                        reqAr =>
                        {
                            using (var requestStream = req.EndGetRequestStream(reqAr))
                            {
                                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                            }

                            req.BeginGetResponse(
                                new AsyncCallback(
                                    resAr =>
                                    {
                                        string screenName = string.Empty;
                                        string userID = string.Empty;

                                        var twitterResponse = new TwitterAsyncResponse<UserIdentifier>();

                                        try
                                        {
                                            string accessTokenResponse = string.Empty;

                                            var res = req.EndGetResponse(resAr) as HttpWebResponse;

                                            using (var respStream = res.GetResponseStream())
                                            using (var respReader = new StreamReader(respStream))
                                            {
                                                accessTokenResponse = respReader.ReadToEnd();
                                            }

                                            ProcessAccessTokenResponse(ref screenName, ref userID, accessTokenResponse);
                                        }
                                        catch (TwitterQueryException tqe)
                                        {
                                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                                            twitterResponse.Message = "Error while communicating with Twitter. Please see Error property for details.";
                                            twitterResponse.Exception = tqe;
                                        }
                                        catch (Exception ex)
                                        {
                                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                                            twitterResponse.Message = "Error during LINQ to Twitter processing. Please see Error property for details.";
                                            twitterResponse.Exception = ex;
                                        }
                                        finally
                                        {
                                            if (authenticationCompleteCallback != null)
                                            {
                                                twitterResponse.State =
                                                    new UserIdentifier
                                                    {
                                                        ID = userID,
                                                        UserID = userID,
                                                        ScreenName = screenName
                                                    };
                                                authenticationCompleteCallback(twitterResponse);
                                            }
                                        }
                                    }), null);
                        }), null);
#else
            Exception asyncException = null;

            using (var resetEvent = new ManualResetEvent(/*initialStateSignaled:*/ false))
            {
                req.BeginGetRequestStream(
                    new AsyncCallback(
                        ar =>
                        {
                            try
                            {
                                using (var requestStream = req.EndGetRequestStream(ar))
                                {
                                    requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                                }
                            }
                            catch (Exception ex)
                            {
                                asyncException = ex;
                            }
                            finally
                            {
                                resetEvent.Set();
                            }
                        }), null);

                resetEvent.WaitOne();
            }

            if (asyncException != null)
            {
                throw asyncException;
            }

            req.BeginGetResponse(
                new AsyncCallback(
                    ar =>
                    {
                        string screenName = string.Empty;
                        string userID = string.Empty;

                        var twitterResponse = new TwitterAsyncResponse<UserIdentifier>();

                        try
                        {
                            string accessTokenResponse = string.Empty;

                            var res = req.EndGetResponse(ar) as HttpWebResponse;

                            using (var respStream = res.GetResponseStream())
                            using (var respReader = new StreamReader(respStream))
                            {
                                accessTokenResponse = respReader.ReadToEnd();
                            }

                            ProcessAccessTokenResponse(ref screenName, ref userID, accessTokenResponse);
                        }
                        catch (TwitterQueryException tqe)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error while communicating with Twitter. Please see Error property for details.";
                            twitterResponse.Exception = tqe;
                        }
                        catch (Exception ex)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error during LINQ to Twitter processing. Please see Error property for details.";
                            twitterResponse.Exception = ex;
                        }
                        finally
                        {
                            if (authenticationCompleteCallback != null)
                            {
                                twitterResponse.State =
                                    new UserIdentifier
                                    {
                                        ID = userID,
                                        UserID = userID,
                                        ScreenName = screenName
                                    };
                                authenticationCompleteCallback(twitterResponse);
                            }
                        }
                    }), null);
#endif
        }
Пример #9
0
        /// <summary>
        /// Asynchronous request for OAuth access token
        /// </summary>
        /// <param name="verifier">Verification token provided by Twitter after user authorizes (7-digit number for Pin authorization too)</param>
        /// <param name="oauthAccessTokenUrl">Access token URL</param>
        /// <param name="twitterCallbackUrl">URL for your app that Twitter redirects to after authorization (null for Pin authorization)</param>
        /// <param name="authenticationCompleteCallback">Callback to application after response completes (contains UserID and ScreenName)</param>
        public void GetAccessTokenAsync(
            string verifier,
            Uri oauthAccessTokenUrl,
            string twitterCallbackUrl,
            AuthAccessType authAccessType,
            Action<TwitterAsyncResponse<UserIdentifier>> authenticationCompleteCallback)
        {
            OAuthVerifier = verifier;

            var req = GetHttpGetRequest(oauthAccessTokenUrl, twitterCallbackUrl, authAccessType);

            req.BeginGetResponse(
                new AsyncCallback(
                    ar =>
                    {
                        string screenName = string.Empty;
                        string userID = string.Empty;

                        var twitterResponse = new TwitterAsyncResponse<UserIdentifier>();

                        try
                        {

                            var res = req.EndGetResponse(ar) as HttpWebResponse;

                            string accessTokenResponse = GetHttpResponse(res);

                            ProcessAccessTokenResponse(ref screenName, ref userID, accessTokenResponse);
                        }
                        catch (TwitterQueryException tqe)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error while communicating with Twitter. Please see Error property for details.";
                            twitterResponse.Exception = tqe;
                        }
                        catch (Exception ex)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error during LINQ to Twitter processing. Please see Error property for details.";
                            twitterResponse.Exception = ex;
                        }
                        finally
                        {
                            if (authenticationCompleteCallback != null)
                            {
                                twitterResponse.State =
                                    new UserIdentifier
                                    {
                                        ID = userID,
                                        UserID = userID,
                                        ScreenName = screenName
                                    };
                                authenticationCompleteCallback(twitterResponse); 
                            }
                        }
                    }), null);

        }
Пример #10
0
        /// <summary>
        /// Asynchronous request for OAuth request token
        /// </summary>
        /// <param name="oauthRequestTokenUrl">Url to make initial request on</param>
        /// <param name="oauthAuthorizeUrl">Url to send user to for authorization</param>
        /// <param name="twitterCallbackUrl">Url for Twitter to redirect to after authorization (null for Pin authorization)</param>
        /// <param name="forceLogin">Should user be forced to log in to authorize this app</param>
        /// <param name="authorizationCallback">Lambda to let program perform redirect to authorization page</param>
        /// <param name="authenticationCompleteCallback">Lambda to invoke to let user know when authorization completes</param>
        public void GetRequestTokenAsync(
            Uri oauthRequestTokenUrl, 
            Uri oauthAuthorizeUrl, 
            string twitterCallbackUrl, 
            AuthAccessType authAccessType,
            bool forceLogin, 
            Action<string> authorizationCallback, 
            Action<TwitterAsyncResponse<object>> authenticationCompleteCallback)
        {
            var req = GetHttpGetRequest(oauthRequestTokenUrl, twitterCallbackUrl, authAccessType);

            req.BeginGetResponse(
                new AsyncCallback(
                    ar =>
                    {
                        var twitterResponse = new TwitterAsyncResponse<object>();

                        try
                        {
                            var res = req.EndGetResponse(ar) as HttpWebResponse;

                            string requestTokenResponse = GetHttpResponse(res);

                            string authorizationUrl = PrepareAuthorizeUrl(oauthAuthorizeUrl.ToString(), forceLogin, requestTokenResponse);

                            authorizationCallback(authorizationUrl);
                        }
                        catch (TwitterQueryException tqe)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error while communicating with Twitter. Please see Error property for details.";
                            twitterResponse.Exception = tqe;
                        }
                        catch (Exception ex)
                        {
                            twitterResponse.Status = TwitterErrorStatus.TwitterApiError;
                            twitterResponse.Message = "Error during LINQ to Twitter processing. Please see Error property for details.";
                            twitterResponse.Exception = ex;
                        }
                        finally
                        {
                            if (authenticationCompleteCallback != null)
                            {
                                authenticationCompleteCallback(twitterResponse); 
                            }
                        }
                    }), null);
        }
Пример #11
0
        void UpdateResponse(TwitterAsyncResponse<TwitterStatus> ee)
        {
            if (ee.ResponseObject == null) {
            TweetFailure(ee.Content);
            return;
              }

              InsertTweetIn(ee.ResponseObject, ColumnType.Timeline);
              if (ee.ResponseObject.Text.Contains("@" + Global.ThisUser.ScreenName)) {
            InsertTweetIn(ee.ResponseObject, ColumnType.Mentions);
              }

              this.Invoke(new Action(delegate
              {
            textTweet.Enabled = true;
            textTweet.Text = "";
            textTweet.Focus();

            buttonUpdate.Enabled = true;

            checkMedia.Enabled = true;
            checkMedia.Checked = false;
              }));
        }
Пример #12
0
 public void ProcessTweetsCallback(TwitterAsyncResponse<TwitterStatusCollection> ee)
 {
     TwitterStatusCollection tweets = ee.ResponseObject;
       if (tweets == null) {
     Global.DeckForm.TweetFailure(ee.Content);
     return;
       }
       for (int i = tweets.Count - 1; i >= 0; i--) {
     InsertTweet(flowColumn, tweets[i]);
       }
 }
Пример #13
0
 public void ProcessDMCallback(TwitterAsyncResponse<TwitterDirectMessageCollection> ee)
 {
     TwitterDirectMessageCollection dms = ee.ResponseObject;
       if (dms == null) {
     return;
       }
       for (int i = dms.Count - 1; i >= 0; i--) {
     InsertDM(flowColumn, dms[i]);
       }
 }