Search code examples
c#asp.netasp.net-mvc-4oauth-2.0dailymotion-api

Wait for user to complete its Authentication C# asp.net MVC


So I Have A project in which I upload a video to daily motion with API and for uploading a video with api I need an authorized access token.

So the problem I am getting is that when my program starts the authorizing process it didn't wait for a user to complete its authorizing and exchange the token it goes to a new step.

hope you understand my problem


  [HttpPost]
        public ActionResult Add(Video Videos)
        {
            var accessToken = GetAccessToken(); 

            Authorize(accessToken);//should wait here for user to complete the 
                                   // process and then move to the next step




            var fileToUpload = @"C:\Users\Hassan\Downloads\SampleVideo_1280x720_1mb.mp4";



            var uploadUrl = GetFileUploadUrl(accessToken);



            var response = GetFileUploadResponse(fileToUpload, accessToken, uploadUrl);






            var uploadedResponse = PublishVideo(response, accessToken,Videos.VideoName,Videos.Tags);
            var TEST = Privateid(response, accessToken,uploadedResponse.id);



            Videos.PrivateID = TEST.private_id;
            Videos.VideoEmbeddedCode = TEST.embed_html;
            _context.Videos.Add(Videos);
            _context.SaveChanges();




            return RedirectToAction("AddVideo", "Admin");
        }

GetAccessToken and Authroize Method

 private static string GetAccessToken()
    {
        var request = WebRequest.Create("https://api.dailymotion.com/oauth/token");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        var requestString = String.Format("grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}",
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["Key"]),
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["secret"]),
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["usernameAPI"]),
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["passwordAPI"]));

        var requestBytes = Encoding.UTF8.GetBytes(requestString);

        var requestStream = request.GetRequestStream();

        requestStream.Write(requestBytes, 0, requestBytes.Length);

        var response = request.GetResponse();

        var responseStream = response.GetResponseStream();
        string responseString;
        using (var reader = new StreamReader(responseStream))
        {
            responseString = reader.ReadToEnd();
        }

        var oauthResponse = JsonConvert.DeserializeObject<OAuthResponse>(responseString);

        return oauthResponse.access_token;
    }
    private static void Authorize(string accessToken)
    {
        var authorizeUrl = String.Format("https://api.dailymotion.com/oauth/authorize?response_type=code&client_id={0}&scope=read+write+manage_videos+delete&redirect_uri={1}",
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["Key"]),
            HttpUtility.UrlEncode(ConfigurationManager.AppSettings["callbackurl"]));



        Process.Start(authorizeUrl);

        var client = new WebClient();
        client.Headers.Add("Authorization", "OAuth " + accessToken);


    }

Solution

  • Make the Authorize method async like private static async Task Authorize(string accessToken) and insert an await operator like: await Authorize(accessToken);