Exemple #1
0
        public static async Task <HttpResponseMessage> PostTwitterRequestTokenAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "auth/twitter/request_token")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            dynamic data = await req.Content.ReadAsAsync <object>();

            string returnUrl     = data.returnUrl;
            var    authorization = TwitterHelper.BuildRequestAuthorizationHeader(twitterConfig.RequestTokenUrl, returnUrl, twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret);
            var    httpClient    = new HttpClient();
            var    requestMsg    = new HttpRequestMessage()
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(twitterConfig.RequestTokenUrl)
            };

            requestMsg.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", authorization);

            var response = await httpClient.SendAsync(requestMsg);

            var responseText = await response.Content.ReadAsStringAsync();

            log.Info(responseText);
            if (!response.IsSuccessStatusCode)
            {
                return(req.CreateJsonResponse(response.StatusCode, responseText));
            }

            string oauthToken       = null;
            string oauthTokenSecret = null;

            var keyValPairs = responseText.Split('&');

            for (var i = 0; i < keyValPairs.Length; i++)
            {
                var splits = keyValPairs[i].Split('=');
                switch (splits[0])
                {
                case "oauth_token":
                    oauthToken = splits[1];
                    break;

                case "oauth_token_secret":
                    oauthTokenSecret = splits[1];
                    break;
                }
            }

            return(req.CreateJsonResponse(HttpStatusCode.OK,
                                          new
            {
                oauthToken,
                oauthTokenSecret
            }));
        }