예제 #1
0
        public async Task <RequestTokenResponse> RequestToken(string callbackUrl = null)
        {
            var endpoint   = this.GetOAuthUrl("request_token");
            var parameters = new Query();

            if (!string.IsNullOrEmpty(callbackUrl))
            {
                parameters.Add(OAuthHelper.OAuthParameters.Callback, callbackUrl);
            }

            using var request = WebUtility.CreateOAuthRequest(HttpMethod.Post, endpoint, this.Api, parameters);
            var response = await this.Api.SendRequest <string>(request).ConfigureAwait(false);

            return(new RequestTokenResponse(response));
        }
예제 #2
0
        public async Task <TwitterApi> AccessToken(string requestToken, string verifier)
        {
            if (string.IsNullOrEmpty(requestToken))
            {
                throw new ArgumentNullException(nameof(requestToken));
            }

            if (string.IsNullOrEmpty(verifier))
            {
                throw new ArgumentNullException(nameof(verifier));
            }

            var parameters = new Query
            {
                [OAuthHelper.OAuthParameters.Token]    = requestToken,
                [OAuthHelper.OAuthParameters.Verifier] = verifier,
            };

            var endpoint = this.GetOAuthUrl("access_token");
            var request  = WebUtility.CreateOAuthRequest(HttpMethod.Post, endpoint, this.Api, parameters);
            var response = await this.Api.SendRequest <string>(request).ConfigureAwait(false);

            var values = WebUtility.ParseQueryString(response);

            if (values.TryGetValue("oauth_token", out var oauthToken))
            {
                this.Api.AccessToken = oauthToken;
            }

            if (values.TryGetValue("oauth_token_secret", out var oauthTokenSecret))
            {
                this.Api.AccessTokenSecret = oauthTokenSecret;
            }

            if (values.TryGetValue("user_id", out var userId))
            {
                this.Api.UserId = long.Parse(userId);
            }

            if (values.TryGetValue("screen_name", out var screenName))
            {
                this.Api.ScreenName = screenName;
            }

            return(this.Api);
        }