Beispiel #1
0
        public ITwitterAccount ConnectAccount(string pin)
        {
            string REQUEST_URL = $"https://api.twitter.com/oauth/access_token?oauth_token={RequestToken}&oauth_verifier={pin}";

            OAuthRequest client = OAuthRequest.ForAccessToken(YoutubeClientData.TwitterOauthToken, YoutubeClientData.TwitterOauthTokenSecret, RequestToken, pin);

            client.RequestUrl = REQUEST_URL;

            string         auth    = client.GetAuthorizationHeader();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl);

            request.Headers.Add("Authorization", auth);

            Console.WriteLine("Calling " + REQUEST_URL);

            // make the call and print the string value of the response JSON
            HttpWebResponse response    = (HttpWebResponse)request.GetResponse();
            Stream          dataStream  = response.GetResponseStream();
            StreamReader    reader      = new StreamReader(dataStream);
            string          strResponse = reader.ReadToEnd();

            var queryParams = HttpUtility.ParseQueryString(strResponse);

            return(new TwitterAccount(queryParams["oauth_token"], queryParams["oauth_token_secret"], queryParams["user_id"], queryParams["screen_name"]));
        }
        /// <summary>
        /// Gets an OAuth access token
        /// </summary>
        private OAuthToken GetAccessToken(OAuthToken requestToken, string oauthVerifier)
        {
            var request = OAuthRequest.ForAccessToken(_clientKey, _clientSecret,
                                                      requestToken.Token, requestToken.TokenSecret,
                                                      oauthVerifier);

            request.Method     = "POST";
            request.RequestUrl = _apiUri.AbsoluteUri + ApiEndpoints.AccessToken;

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Add("Authorization", request.GetAuthorizationHeader());

                var response     = http.PostAsync(request.RequestUrl, null).Result;
                var responseBody = response.Content.ReadAsStringAsync().Result;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new BreezyApiException(
                              String.Format("Could not get access token.\r\nResponse status code: {0}.\r\nResponse: {1}",
                                            response.StatusCode, responseBody));
                }

                var content = Util.ParseFormContent(responseBody);
                return(new OAuthToken
                {
                    Token = content["oauth_token"],
                    TokenSecret = content["oauth_token_secret"]
                });
            }
        }
Beispiel #3
0
        public void GetAccess(string verificationCode)
        {
            this.client            = OAuthRequest.ForAccessToken(this.consumer_key, this.consumer_secret, this.request_token, this.request_secret, verificationCode);
            this.client.RequestUrl = this.accessUrl;

            // Using URL query authorization
            string auth2     = this.client.GetAuthorizationQuery();
            var    url2      = this.client.RequestUrl + "?" + auth2;
            var    request2  = (HttpWebRequest)WebRequest.Create(url2);
            var    response2 = (HttpWebResponse)request2.GetResponse();

            //Console.WriteLine(url2);
            //Console.WriteLine("STREAM!");

            //Console.WriteLine(StreamToString(response2.GetResponseStream()));

            string authQuery = StreamToString(response2.GetResponseStream());

            Console.WriteLine(authQuery);

            //Seperate the stream into an Array.
            List <string> query = new List <string>();

            //TODO: Fix ERROR!
            query = QueryToArray(authQuery);
            //Assign each query to their value.
            access_token  = query[3];
            access_secret = query[1];

            //Console.WriteLine("Access Token: " + access_token);
            //Console.WriteLine("Access Secret: " + access_secret);
        }
Beispiel #4
0
        public async Task <AccessTokenInfo> RequestAccessTokens(string authToken, string authTokenSecret, string oauthVerifier)
        {
            var oauthRequest = OAuthRequest.ForAccessToken(
                ConsumerKey,
                ConsumerSecret,
                authToken,
                authTokenSecret,
                oauthVerifier
                );

            oauthRequest.RequestUrl = AccessTokenUrl;
            oauthRequest.Version    = OAuthVersion;

            try
            {
                var request = await(oauthRequest.RequestUrl + "?" + oauthRequest.GetAuthorizationQuery()).GetAsync();
                var result  = await request.Content.ReadAsStringAsync();

                var accessTokens = result.Split('&').Select(x => x.Split('=')).ToDictionary(split => split[0], split => split[1]);
                return(new AccessTokenInfo {
                    AccessToken = accessTokens["oauth_token"], AccessTokenSecret = accessTokens["oauth_token_secret"]
                });
            }
            catch (FlurlHttpException ex)
            {
                throw new UnauthorizedQuickBooksClientException(
                          "Unable to get access tokens.",
                          ex.InnerException);
            }
        }
Beispiel #5
0
        public NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
        {
            // Creating a new instance with a helper method
            var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);

            oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";

            return(HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary <string, string>())).Content));
        }
        public override object RequestAction(string action, IDictionary <string, string> query)
        {
            if (action == "startOAuth")
            {
                if (query["callbackUrl"].IsNullOrWhiteSpace())
                {
                    throw new BadRequestException("QueryParam callbackUrl invalid.");
                }

                var oAuthRequest = OAuthRequest.ForRequestToken(null, null, query["callbackUrl"]);
                oAuthRequest.RequestUrl = Settings.OAuthRequestTokenUrl;
                var qscoll = OAuthQuery(oAuthRequest);

                var url = string.Format("{0}?oauth_token={1}&oauth_callback={2}", Settings.OAuthUrl, qscoll["oauth_token"], query["callbackUrl"]);

                return(new
                {
                    OauthUrl = url,
                    RequestTokenSecret = qscoll["oauth_token_secret"]
                });
            }
            else if (action == "getOAuthToken")
            {
                if (query["oauth_token"].IsNullOrWhiteSpace())
                {
                    throw new BadRequestException("QueryParam oauth_token invalid.");
                }

                if (query["requestTokenSecret"].IsNullOrWhiteSpace())
                {
                    throw new BadRequestException("Missing requestTokenSecret.");
                }

                var oAuthRequest = OAuthRequest.ForAccessToken(null, null, query["oauth_token"], query["requestTokenSecret"], "");
                oAuthRequest.RequestUrl = Settings.OAuthAccessTokenUrl;
                var qscoll = OAuthQuery(oAuthRequest);

                Settings.AccessToken       = qscoll["oauth_token"];
                Settings.AccessTokenSecret = qscoll["oauth_token_secret"];

                var user = GetUser(Settings.UserId);

                return(new
                {
                    Settings.AccessToken,
                    Settings.AccessTokenSecret,
                    RequestTokenSecret = "",
                    UserId = user.Item1,
                    UserName = user.Item2
                });
            }

            return(new { });
        }
Beispiel #7
0
        public OAuthToken GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
        {
            // Creating a new instance with a helper method
            var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);

            oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
            var qscoll = OAuthQuery(oAuthRequest);

            return(new OAuthToken
            {
                AccessToken = qscoll["oauth_token"],
                AccessTokenSecret = qscoll["oauth_token_secret"]
            });
        }
        public async Task <AccessTokenInfo> GetAccessToken(RequestTokenInfo requestTokenInfo)
        {
            var client = OAuthRequest.ForAccessToken(
                Resources.ConsumerKey,
                Resources.ConsumerSecret,
                requestTokenInfo.RequestToken,
                requestTokenInfo.RequestSecret,
                requestTokenInfo.Verifier);

            client.RequestUrl = Resources.AccessTokenURL;

            var accessTokenString = await Tools.GetStringResponse(client.RequestUrl + "?" + client.GetAuthorizationQuery());

            var tokenValues = GetTokenValues(accessTokenString);

            var accessTokenInfo = new AccessTokenInfo();

            accessTokenInfo.AccessToken       = tokenValues[0];
            accessTokenInfo.AccessTokenSecret = tokenValues[1];

            return(accessTokenInfo);
        }
        public static OauthToken GetAccesToken(AvansOauthHelperOptions options, OauthToken requestToken, string verifier)
        {
            // Creating a new instance with a helper method
            OAuthRequest client = OAuthRequest.ForAccessToken(options.AvansClientId, options.AvansSecret, requestToken.Token, requestToken.Secret);

            client.Verifier   = verifier;
            client.RequestUrl = "https://publicapi.avans.nl/oauth/access_token";

            // Using URL query authorization to get the request token
            string auth     = client.GetAuthorizationQuery();
            var    url      = client.RequestUrl + "?" + auth;
            var    request  = (HttpWebRequest)WebRequest.Create(url);
            var    response = (HttpWebResponse)request.GetResponse();

            Stream       receiveStream = response.GetResponseStream();
            StreamReader reader        = new StreamReader(receiveStream, Encoding.UTF8);
            string       body          = reader.ReadToEnd();

            //turn body into uri
            var uri = "http://temp?" + body;

            return(AvansOauthHelper.getTokenFormUri(uri));
        }
Beispiel #10
0
        private static OAuthCredentials GenerateOAuthAccessToken(string consumerKey, string secret)
        {
            string baseUrl      = "http://api.smugmug.com";
            string requestUrl   = "/services/oauth/1.0a/getRequestToken";
            string authorizeUrl = "/services/oauth/1.0a/authorize";
            string accessUrl    = "/services/oauth/1.0a/getAccessToken";

            string requestToken       = null;
            string requestTokenSecret = null;
            string accesstoken        = null;
            string accessTokenSecret  = null;

            #region Request Token
            OAuthRequest oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, secret, "oob");
            oAuthRequest.RequestUrl = baseUrl + requestUrl;
            string         auth    = oAuthRequest.GetAuthorizationHeader();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(oAuthRequest.RequestUrl);
            request.Headers.Add("Authorization", auth);
            HttpWebResponse response       = (HttpWebResponse)request.GetResponse();
            Stream          responseStream = response.GetResponseStream();
            StreamReader    readStream     = new StreamReader(responseStream, System.Text.Encoding.UTF8);
            string          result         = readStream.ReadToEnd();
            foreach (string token in result.Split('&'))
            {
                string[] splitToken = token.Split('=');

                switch (splitToken[0])
                {
                case "oauth_token":
                    requestToken = splitToken[1];
                    break;

                case "oauth_token_secret":
                    requestTokenSecret = splitToken[1];
                    break;

                default:
                    break;
                }
            }
            response.Close();
            #endregion

            #region Authorization
            string authorizationUrl = String.Format("{0}{1}?mode=auth_req_token&oauth_token={2}&Access=Full&Permissions=Modify", baseUrl, authorizeUrl, requestToken);
            System.Diagnostics.Process.Start(authorizationUrl);

            Console.WriteLine("Enter the six-digit code: ");
            string verifier = Console.ReadLine();
            #endregion

            #region Access Token
            oAuthRequest            = OAuthRequest.ForAccessToken(consumerKey, secret, requestToken, requestTokenSecret, verifier);
            oAuthRequest.RequestUrl = baseUrl + accessUrl;
            auth    = oAuthRequest.GetAuthorizationHeader();
            request = (HttpWebRequest)WebRequest.Create(oAuthRequest.RequestUrl);
            request.Headers.Add("Authorization", auth);
            response       = (HttpWebResponse)request.GetResponse();
            responseStream = response.GetResponseStream();
            readStream     = new StreamReader(responseStream, System.Text.Encoding.UTF8);
            result         = readStream.ReadToEnd();
            foreach (string token in result.Split('&'))
            {
                string[] splitToken = token.Split('=');

                switch (splitToken[0])
                {
                case "oauth_token":
                    accesstoken = splitToken[1];
                    break;

                case "oauth_token_secret":
                    accessTokenSecret = splitToken[1];
                    break;

                default:
                    break;
                }
            }
            response.Close();
            #endregion

            return(new OAuthCredentials(consumerKey, secret, accesstoken, accessTokenSecret));
        }