/// <summary>
        /// After the user Authorizes the app, Twitter will
        /// redirect to the callback url, provided during
        /// BeginAuthorization. When redirecting, Twitter will
        /// also provide oauth_verifier and oauth_token
        /// parameters. This method uses those parameters to
        /// request an access token, which is used automatically
        /// by LINQ to Twitter when executing queries.
        /// </summary>
        /// <param name="callback">URL that Twitter redirected to after authorization</param>
        /// <returns>True if successful</returns>
        public bool CompleteAuthorization(Uri callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback", "You must pass in the callback that Twitter returned after authentication.");
            }

            if (IsAuthorized)
            {
                return(true);
            }

            string verifier = OAuthTwitter.GetUrlParamValue(callback.Query, "oauth_verifier");

            if (verifier != null)
            {
                string oAuthToken = OAuthTwitter.GetUrlParamValue(callback.Query, "oauth_token");

                string screenName;
                string userID;
                OAuthTwitter.AccessTokenGet(oAuthToken, verifier, OAuthAccessTokenUrl, string.Empty, out screenName, out userID);

                ScreenName = screenName;
                UserId     = userID;

                Credentials.OAuthToken  = OAuthTwitter.OAuthToken;
                Credentials.AccessToken = OAuthTwitter.OAuthTokenSecret;
                Credentials.ScreenName  = screenName;
                Credentials.UserId      = userID;
            }

            return(IsAuthorized);
        }
Exemple #2
0
        /// <summary>
        /// Retrieves access token from Twitter.
        /// Call this after calling GetAuthorizationPageLink()
        /// </summary>
        /// <param name="oAuthToken">Auth Token from call to GetAuthorizationPageLink</param>
        public void RetrieveAccessToken(string oAuthToken)
        {
            if (string.IsNullOrEmpty(oAuthToken))
            {
                throw new ArgumentException("Invalid OAuth Token.", "oAuthToken");
            }

            string screenName;
            string userID;

            OAuthTwitter.AccessTokenGet(oAuthToken, OAuthAccessTokenUrl, out screenName, out userID);

            OAuthRequestScreenName = screenName;
            OAuthRequestUserID     = userID;
        }
        /// <summary>
        /// Perform authorization
        /// </summary>
        /// <param name="forceLogin">Force the user to enter their name.</param>
        public void Authorize(bool forceLogin)
        {
            if (IsAuthorized)
            {
                return;
            }

            if (GetPin == null)
            {
                throw new InvalidOperationException("GetPin must have a handler before calling Authorize.");
            }

            if (GoToTwitterAuthorization == null)
            {
                throw new InvalidOperationException("GoToTwitterAuthorization must have a handler before calling Authorize.");
            }

            string link = OAuthTwitter.AuthorizationLinkGet(OAuthRequestTokenUrl, OAuthAuthorizeUrl, "oob", forceLogin, AuthAccessType);

            GoToTwitterAuthorization(link);

            string verifier = GetPin();

            // TODO: Refactor to share similar logic with WebAuthorizer
            string oAuthToken =
                (from nameValPair in new Uri(link).Query.TrimStart('?').Split('&')
                 let pair = nameValPair.Split('=')
                            where pair[0] == "oauth_token"
                            select pair[1])
                .SingleOrDefault();

            string screenName;
            string userID;

            OAuthTwitter.AccessTokenGet(oAuthToken, verifier, OAuthAccessTokenUrl, string.Empty, out screenName, out userID);

            ScreenName = screenName;
            UserId     = userID;

            Credentials.OAuthToken  = OAuthTwitter.OAuthToken;
            Credentials.AccessToken = OAuthTwitter.OAuthTokenSecret;
            Credentials.ScreenName  = screenName;
            Credentials.UserId      = userID;
        }