C# 将文件上载到Windows应用商店应用程序中的Google Drive
更新1 我认为我使用了不正确的URL,表示使用“”&表示使用“”。虽然我收到了同样的400个错误请求 我可以在后台上传器类中使用Google Drive upload REST API吗C# 将文件上载到Windows应用商店应用程序中的Google Drive,c#,windows-8,windows-runtime,microsoft-metro,google-drive-api,C#,Windows 8,Windows Runtime,Microsoft Metro,Google Drive Api,更新1 我认为我使用了不正确的URL,表示使用“”&表示使用“”。虽然我收到了同样的400个错误请求 我可以在后台上传器类中使用Google Drive upload REST API吗 我从谷歌驱动器以下上传文件,但我得到400-错误的要求。我的代码怎么了 public static async Task UploadFileAsync(Token AuthToken, StorageFile file, DriveFile objFolder) { try {
我从谷歌驱动器以下上传文件,但我得到400-错误的要求。我的代码怎么了
public static async Task UploadFileAsync(Token AuthToken, StorageFile file, DriveFile objFolder)
{
try
{
if (!httpClient.DefaultRequestHeaders.Contains("Authorization"))
{
httpClient.DefaultRequestHeaders.Add("Authorization", AuthToken.TokenType + " " + AuthToken.AccessToken);
}
var JsonMessage = JsonConvert.SerializeObject(objFolder);
/*JsonMessage = {"title":"c4611_sample_explain.pdf","mimeType":"application/pdf","parents":[{"id":"root","kind":"drive#fileLink"}]}*/
var JsonReqMsg = new StringContent(JsonMessage, Encoding.UTF8, "application/json");
var fileBytes = await file.ToBytesAsync();
var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(fileBytes));
form.Add(JsonReqMsg);
form.Headers.ContentType = new MediaTypeHeaderValue("multipart/related");
var UploadReq = await httpClient.PostAsync(new Uri("https://www.googleapis.com/drive/v2/files?uploadType=multipart"), form);
if (UploadReq.IsSuccessStatusCode)
{
var UploadRes = await UploadReq.Content.ReadAsStringAsync();
}
else
{
}
}
catch (Exception ex)
{
}
}
你必须使用
我这里有一个工作示例(对不起,JSON字符串是硬编码的):
//多部分文件上载
HttpClient=新的HttpClient();
字符串URI字符串=”https://www.googleapis.com/upload/drive/v2/files?key=&access_token=&uploadType=multipart";
Uri=新Uri(uriString);
HttpContent metadataPart=新的StringContent(
“{\“title\”:\“我的文件\”}”,
Encoding.UTF8,
“应用程序/json”);
HttpContent mediaPart=新的StringContent(
“顽皮的兔子把所有的饼干都吃了。”,
Encoding.UTF8,
“文本/纯文本”);
MultipartContent MultipartContent=新MultipartContent();
multipartContent.Add(metadataPart);
multipartContent.Add(mediaPart);
HttpResponseMessage response=wait client.PostAsync(uri,multipartContent);
string responseString=wait response.Content.ReadAsStringAsync();
只是猜测-很多时候,“400错误请求”表示身份验证有问题(用户名/密码/其他无效)。这里有一个例子:。我猜,问题可能与此相关,因为您使用的是MultipartFormDataContent
而不是MultipartContent
,并且内容类型:multipart/related
可能会被覆盖。在这种情况下,小提琴手的痕迹会有所帮助。
// Multipart file upload
HttpClient client = new HttpClient();
string uriString = "https://www.googleapis.com/upload/drive/v2/files?key=<your-key>&access_token=<access-token>&uploadType=multipart";
Uri uri = new Uri(uriString);
HttpContent metadataPart = new StringContent(
"{ \"title\" : \"My File\"}",
Encoding.UTF8,
"application/json");
HttpContent mediaPart = new StringContent(
"The naughty bunny ate all the cookies.",
Encoding.UTF8,
"text/plain");
MultipartContent multipartContent = new MultipartContent();
multipartContent.Add(metadataPart);
multipartContent.Add(mediaPart);
HttpResponseMessage response = await client.PostAsync(uri, multipartContent);
string responseString = await response.Content.ReadAsStringAsync();