等待用户完成其身份验证 C# asp.net MVC
Wait for user to complete its Authentication C# asp.net MVC
所以我有一个项目,我在其中使用 API 将视频上传到 daily motion 并使用 api 上传视频,我需要授权访问令牌。
所以我遇到的问题是,当我的程序开始授权过程时,它没有等待用户完成授权并交换令牌,而是进入了新的步骤。
希望你能理解我的问题
[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 和 Authroize 方法
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);
}
使授权方法 async
像 private static async Task Authorize(string accessToken)
并插入一个 await 运算符,如:await Authorize(accessToken);
所以我有一个项目,我在其中使用 API 将视频上传到 daily motion 并使用 api 上传视频,我需要授权访问令牌。
所以我遇到的问题是,当我的程序开始授权过程时,它没有等待用户完成授权并交换令牌,而是进入了新的步骤。
希望你能理解我的问题
[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 和 Authroize 方法
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);
}
使授权方法 async
像 private static async Task Authorize(string accessToken)
并插入一个 await 运算符,如:await Authorize(accessToken);