Create() публичный статический Метод

Makes an instance of Tokens. Tokens.Create will not fetch UserId and ScreenName automatically. If you need them, call Rest.Account.VerifyCredentials(object).
public static Create ( string consumerKey, string consumerSecret, string accessToken, string accessSecret, long userID, string screenName = null ) : Tokens
consumerKey string The consumer key.
consumerSecret string The consumer secret.
accessToken string The access token.
accessSecret string The access secret.
userID long The user's ID.
screenName string The user's screen name.
Результат Tokens
Пример #1
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="pin">The pin code.</param>
        /// <returns>The tokens.</returns>
        public static Tokens GetTokens(this OAuthSession session, string pin)
        {
            var reqUrl = GetAccessTokenUrl(session.ConnectionOptions);
            var prm    = new Dictionary <string, object>()
            {
                { "oauth_verifier", pin }
            };
            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, session.ConnectionOptions).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 = session.ConnectionOptions;
                return(t);
            }
            catch (WebException ex)
            {
                var tex = TwitterException.Create(ex);
                if (tex != null)
                {
                    throw tex;
                }
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// <para>Generates the authorize URI as an asynchronous operation.</para>
        /// <para>Then call <see cref="CoreTweet.OAuth.GetTokensAsync"/> 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>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// <para>The task object representing the asynchronous operation.</para>
        /// <para>The Result property on the task object returns the authorize URI.</para>
        /// </returns>
        public static Task <OAuthSession> AuthorizeAsync(string consumerKey, string consumerSecret, string oauthCallback = "oob", ConnectionOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var reqUrl = GetRequestTokenUrl(options);
            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.Get, reqUrl, prm);

            return(Request.HttpGetAsync(reqUrl, prm, header, options, cancellationToken)
                   .ResponseCallback(cancellationToken)
                   .ContinueWith(
                       t => InternalUtils.ReadResponse(t, s =>
            {
                var dic = s.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
                };
            }, cancellationToken),
                       cancellationToken
                       ).Unwrap());
        }
Пример #3
0
        /// <summary>
        /// <para>Gets the OAuth tokens as an asynchronous operation.</para>
        /// <para>Be sure to call <see cref="CoreTweet.OAuth.AuthorizeAsync"/> before call this method.</para>
        /// </summary>
        /// <param name="session">The OAuth session.</param>
        /// <param name="pin">The pin code.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// <para>The task object representing the asynchronous operation.</para>
        /// <para>The Result property on the task object returns the tokens.</para>
        /// </returns>
        public static Task <Tokens> GetTokensAsync(this OAuthSession session, string pin, CancellationToken cancellationToken = default(CancellationToken))
        {
            var reqUrl = GetAccessTokenUrl(session.ConnectionOptions);
            var prm    = new Dictionary <string, object>()
            {
                { "oauth_verifier", pin }
            };
            var header = Tokens.Create(session.ConsumerKey, session.ConsumerSecret, session.RequestToken, session.RequestTokenSecret)
                         .CreateAuthorizationHeader(MethodType.Get, reqUrl, prm);

            return(Request.HttpGetAsync(reqUrl, prm, header, session.ConnectionOptions, cancellationToken)
                   .ResponseCallback(cancellationToken)
                   .ContinueWith(
                       t => InternalUtils.ReadResponse(t, s =>
            {
                var dic = s.Split('&')
                          .Where(z => z.Contains("="))
                          .Select(z => z.Split('='))
                          .ToDictionary(z => z[0], z => z[1]);
                var token = Tokens.Create(session.ConsumerKey, session.ConsumerSecret,
                                          dic["oauth_token"], dic["oauth_token_secret"], long.Parse(dic["user_id"]), dic["screen_name"]);
                token.ConnectionOptions = session.ConnectionOptions;
                return token;
            }, cancellationToken),
                       cancellationToken
                       ).Unwrap());
        }
Пример #4
0
        /// <summary>
        ///     Generates the authorize URI.
        ///     Then call GetTokens(string) after get the pin code.
        /// </summary>
        /// <returns>
        ///     The authorize URI.
        /// </returns>
        /// <param name="consumerKey">
        ///     Consumer key.
        /// </param>
        /// <param name="consumerSecret">
        ///     Consumer secret.
        /// </param>
        /// <param name="oauthCallback">
        ///     <para>For OAuth 1.0a compliance this parameter is required. 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. Set this to oob for out-of-band pin mode. 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="proxy">
        ///     Proxy information for the request.
        /// </param>
        public static OAuthSession Authorize(string consumerKey, string consumerSecret, string oauthCallback = "oob", IWebProxy proxy = null)
        {
            // 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.Get, RequestTokenUrl, prm);
            var dic = from x in Request.HttpGet(RequestTokenUrl, prm, header, "CoreTweet", proxy).Use()
                      from y in new StreamReader(x).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,
                Proxy = proxy
            });
        }
Пример #5
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;
            }
        }
Пример #6
0
        /// <summary>
        ///     Gets the OAuth tokens.
        ///     Be sure to call GenerateAuthUri(string,string) before call this.
        /// </summary>
        /// <param name='pin'>
        ///     Pin code.
        /// </param>
        /// <param name='session'>
        ///     OAuth session.
        /// </para>
        /// <returns>
        ///     The tokens.
        /// </returns>
        public static Tokens GetTokens(this OAuthSession session, string pin)
        {
            var prm = new Dictionary <string, object>()
            {
                { "oauth_verifier", pin }
            };
            var header = Tokens.Create(session.ConsumerKey, session.ConsumerSecret, session.RequestToken, session.RequestTokenSecret)
                         .CreateAuthorizationHeader(MethodType.Get, AccessTokenUrl, prm);
            var dic = from x in Request.HttpGet(AccessTokenUrl, prm, header, "CoreTweet", session.Proxy).Use()
                      from y in new StreamReader(x).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.Proxy = session.Proxy;
            return(t);
        }