admin管理员组

文章数量:1122850

远程服务器返回错误: (403) 已禁止.,c#

因此,我正在使用Dailymotion API上传视频,并使用从GitHub获得的代码,它可以正常工作,但是在4个视频之后,出现了以下异常:

{“远程服务器返回错误:(403)禁止。”}

我在PublishVideo方法中遇到错误

var response = request.GetResponse();

主要代号

var accessToken = GetAccessToken();

Authorize(accessToken);

Console.WriteLine("Access token is " + accessToken);

var fileToUpload = @"E:\Courses\[FreeCourseSite] Udemy - Entity Framework in Depth The Complete Guide\3. Building a Model using Database-First Workflow\11. Summary.mp4";

Console.WriteLine("File to upload is " + fileToUpload);

var uploadUrl = GetFileUploadUrl(accessToken);

Console.WriteLine("Posting to " + uploadUrl);

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

Console.WriteLine("Response:\n");

Console.WriteLine(response + "\n");

Console.WriteLine("Publishing video.\n");

var uploadedResponse = PublishVideo(response, accessToken);

Console.WriteLine(uploadedResponse);

Console.WriteLine("Done. Press enter to exit.");

Console.ReadLine();

}

private static UploadResponse GetFileUploadResponse(string fileToUpload, string accessToken, string uploadUrl)

{

var client = new WebClient();

client.Headers.Add("Authorization", "OAuth " + accessToken);

var responseBytes = client.UploadFile(uploadUrl, fileToUpload);

var responseString = Encoding.UTF8.GetString(responseBytes);

var response = JsonConvert.DeserializeObject(responseString);

return response;

}

private static UploadedResponse PublishVideo(UploadResponse uploadResponse, string accessToken)

{

var request = WebRequest.Create("=" + HttpUtility.UrlEncode(uploadResponse.url));

request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";

request.Headers.Add("Authorization", "OAuth " + accessToken);

var requestString = String.Format("title={0}&tags={1}&channel={2}&private={3}&published={4}",

HttpUtility.UrlEncode("123123123"),

HttpUtility.UrlEncode("tag1"),

HttpUtility.UrlEncode("news"),

HttpUtility.UrlEncode("true"),

HttpUtility.UrlEncode("true"));

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 uploadedResponse = JsonConvert.DeserializeObject(responseString);

return uploadedResponse;

}

private static string GetAccessToken()

{

var request = WebRequest.Create("");

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(SettingsProvider.Key),

HttpUtility.UrlEncode(SettingsProvider.Secret),

HttpUtility.UrlEncode(SettingsProvider.Username),

HttpUtility.UrlEncode(SettingsProvider.Password));

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(responseString);

return oauthResponse.access_token;

}

private static void Authorize(string accessToken)

{

var authorizeUrl = String.Format("=code&client_id={0}&scope=read+write+manage_videos+delete&redirect_uri={1}",

HttpUtility.UrlEncode(SettingsProvider.Key),

HttpUtility.UrlEncode(SettingsProvider.CallbackUrl));

Console.WriteLine("We need permissions to upload. Press enter to open web browser.");

Console.ReadLine();

Process.Start(authorizeUrl);

var client = new WebClient();

client.Headers.Add("Authorization", "OAuth " + accessToken);

Console.WriteLine("Press enter once you have authenticated and been redirected to your callback URL");

Console.ReadLine();

}

private static string GetFileUploadUrl(string accessToken)

{

var client = new WebClient();

client.Headers.Add("Authorization", "OAuth " + accessToken);

var urlResponse = client.DownloadString("");

var response = JsonConvert.DeserializeObject(urlResponse).upload_url;

return response;

}

}

本文标签: 远程服务器返回错误 (403) 已禁止c