Exception throwed by Twitter.
Inheritance: System.Exception, ITwitterResponse
Esempio n. 1
0
 /// <summary>
 /// Invalidates the OAuth 2 Bearer Token.
 /// </summary>
 /// <param name="tokens">An instance of <see cref="OAuth2Token"/>.</param>
 /// <returns>The invalidated token.</returns>
 public static string InvalidateToken(this OAuth2Token tokens)
 {
     try
     {
         return(from x in Request.HttpPost(
                    GetInvalidateTokenUrl(tokens.ConnectionOptions),
                    new Dictionary <string, object>()
         {
             { "access_token", Uri.UnescapeDataString(tokens.BearerToken) }
         },
                    CreateCredentials(tokens.ConsumerKey, tokens.ConsumerSecret),
                    tokens.ConnectionOptions).Use()
                from y in new StreamReader(x.GetResponseStream()).Use()
                select(string) JObject.Parse(y.ReadToEnd())["access_token"]);
     }
     catch (WebException ex)
     {
         var tex = TwitterException.Create(ex);
         if (tex != null)
         {
             throw tex;
         }
         throw;
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Gets the OAuth 2 Bearer Token.
 /// </summary>
 /// <param name="consumerKey">The consumer key.</param>
 /// <param name="consumerSecret">The consumer secret.</param>
 /// <param name="options">The connection options for the request.</param>
 /// <returns>The tokens.</returns>
 public static OAuth2Token GetToken(string consumerKey, string consumerSecret, ConnectionOptions options = null)
 {
     if (options == null)
     {
         options = new ConnectionOptions();
     }
     try
     {
         var token = from x in Request.HttpPost(
             GetAccessTokenUrl(options),
             new Dictionary <string, object>()
         {
             { "grant_type", "client_credentials" }
         },                                                                                           //  At this time, only client_credentials is allowed.
             CreateCredentials(consumerKey, consumerSecret),
             options).Use()
                     from y in new StreamReader(x.GetResponseStream()).Use()
                     select(string) JObject.Parse(y.ReadToEnd())["access_token"];
         var t = OAuth2Token.Create(consumerKey, consumerSecret, token);
         t.ConnectionOptions = options;
         return(t);
     }
     catch (WebException ex)
     {
         var tex = TwitterException.Create(ex);
         if (tex != null)
         {
             throw tex;
         }
         throw;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// <para>Generates the authorize URI.</para>
        /// <para>Then call <see cref="GetTokens"/> after get the pin code.</para>
        /// </summary>
        /// <param name="consumerKey">The Consumer key.</param>
        /// <param name="consumerSecret">The Consumer secret.</param>
        /// <param name="oauthCallback">
        /// <para>For OAuth 1.0a compliance this parameter is required.</para>
        /// <para>The value you specify here will be used as the URL a user is redirected to should they approve your application's access to their account.</para>
        /// <para>Set this to oob for out-of-band pin mode.</para>
        /// <para>This is also how you specify custom callbacks for use in desktop/mobile applications.</para>
        /// <para>Always send an oauth_callback on this step, regardless of a pre-registered callback.</para>
        /// </param>
        /// <param name="options">The connection options for the request.</param>
        /// <returns>The authorize URI.</returns>
        public static OAuthSession Authorize(string consumerKey, string consumerSecret, string oauthCallback = "oob", ConnectionOptions options = null)
        {
            if (options == null)
            {
                options = new ConnectionOptions();
            }
            var reqUrl = GetRequestTokenUrl(options);
            // Note: Twitter says,
            // "If you're using HTTP-header based OAuth, you shouldn't include oauth_* parameters in the POST body or querystring."
            var prm = new Dictionary <string, object>();

            if (!string.IsNullOrEmpty(oauthCallback))
            {
                prm.Add("oauth_callback", oauthCallback);
            }
            var header = Tokens.Create(consumerKey, consumerSecret, null, null)
                         .CreateAuthorizationHeader(MethodType.Post, reqUrl, prm);

            try
            {
                var dic = from x in Request.HttpPost(reqUrl, prm, header, options).Use()
                          from y in new StreamReader(x.GetResponseStream()).Use()
                          select y.ReadToEnd()
                          .Split('&')
                          .Where(z => z.Contains('='))
                          .Select(z => z.Split('='))
                          .ToDictionary(z => z[0], z => z[1]);

                return(new OAuthSession()
                {
                    RequestToken = dic["oauth_token"],
                    RequestTokenSecret = dic["oauth_token_secret"],
                    ConsumerKey = consumerKey,
                    ConsumerSecret = consumerSecret,
                    ConnectionOptions = options
                });
            }
            catch (WebException ex)
            {
                var tex = TwitterException.Create(ex);
                if (tex != null)
                {
                    throw tex;
                }
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// <para>Gets the OAuth tokens.</para>
        /// <para>Be sure to call <see cref="Authorize"/> before call this method.</para>
        /// </summary>
        /// <param name="session">The OAuth session.</param>
        /// <param name="oauthVerifier">The pin code.</param>
        /// <returns>The tokens.</returns>
        public static Tokens GetTokens(this OAuthSession session, string oauthVerifier)
        {
            var options = session.ConnectionOptions ?? new ConnectionOptions();
            var reqUrl  = GetAccessTokenUrl(options);
            var prm     = new Dictionary <string, object>()
            {
                { "oauth_verifier", oauthVerifier }
            };
            var header = Tokens.Create(session.ConsumerKey, session.ConsumerSecret, session.RequestToken, session.RequestTokenSecret)
                         .CreateAuthorizationHeader(MethodType.Post, reqUrl, prm);

            try
            {
                var dic = from x in Request.HttpPost(reqUrl, prm, header, options).Use()
                          from y in new StreamReader(x.GetResponseStream()).Use()
                          select y.ReadToEnd()
                          .Split('&')
                          .Where(z => z.Contains('='))
                          .Select(z => z.Split('='))
                          .ToDictionary(z => z[0], z => z[1]);

                var t = Tokens.Create(session.ConsumerKey, session.ConsumerSecret,
                                      dic["oauth_token"], dic["oauth_token_secret"], long.Parse(dic["user_id"]), dic["screen_name"]);
                t.ConnectionOptions = options;
                return(t);
            }
            catch (WebException ex)
            {
                var tex = TwitterException.Create(ex);
                if (tex != null)
                {
                    throw tex;
                }
                throw;
            }
        }