尝试使用 C# 连接到 Dropbox API v2 在所有尝试中收到 400 错误
Trying to connect to the Dropbox API v2 using C# Receive 400 error on all attempts
新的 Dropbox API 文档位于:
https://blogs.dropbox.com/developers/2015/04/a-preview-of-the-new-dropbox-api-v2/
我正在尝试执行一个简单的元数据调用,但收效甚微。这是我正在使用的代码:
private void go()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization: Bearer xxxxxxxxxxxxxxxxxxx");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"path\": \"/avatar_501.png\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
this.TextBox1.Text = result;
}
}
如有任何帮助,我们将不胜感激!
如果您尝试此代码,您将看到 400 响应的正文,它告诉您 text/json
不是有效的 Content-Type
。我将您的代码转换为控制台应用程序,并使用 Newtonsoft.Json 进行 JSON 序列化。否则,你的代码和我的代码之间的唯一区别是添加了异常处理来获取 400 的主体。
class Program
{
static void Main(string[] args)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization: Bearer <REDACTED>");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(JsonConvert.SerializeObject(new {
path = "/avatar_501.png"
}));
}
HttpWebResponse response;
try
{
response = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (WebException e)
{
response = (HttpWebResponse)e.Response;
}
Console.WriteLine("Status code: {0}", (int)response.StatusCode);
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(streamReader.ReadToEnd());
}
Console.ReadLine();
}
}
输出结果如下:
Status code: 400
Error in call to API function "files/get_metadata": Bad HTTP "Content-Type" header: "text/json". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".
将 Content-Type
更改为 application/json
会导致调用成功。
新的 Dropbox API 文档位于:
https://blogs.dropbox.com/developers/2015/04/a-preview-of-the-new-dropbox-api-v2/
我正在尝试执行一个简单的元数据调用,但收效甚微。这是我正在使用的代码:
private void go()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization: Bearer xxxxxxxxxxxxxxxxxxx");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"path\": \"/avatar_501.png\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
this.TextBox1.Text = result;
}
}
如有任何帮助,我们将不胜感激!
如果您尝试此代码,您将看到 400 响应的正文,它告诉您 text/json
不是有效的 Content-Type
。我将您的代码转换为控制台应用程序,并使用 Newtonsoft.Json 进行 JSON 序列化。否则,你的代码和我的代码之间的唯一区别是添加了异常处理来获取 400 的主体。
class Program
{
static void Main(string[] args)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization: Bearer <REDACTED>");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(JsonConvert.SerializeObject(new {
path = "/avatar_501.png"
}));
}
HttpWebResponse response;
try
{
response = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (WebException e)
{
response = (HttpWebResponse)e.Response;
}
Console.WriteLine("Status code: {0}", (int)response.StatusCode);
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(streamReader.ReadToEnd());
}
Console.ReadLine();
}
}
输出结果如下:
Status code: 400
Error in call to API function "files/get_metadata": Bad HTTP "Content-Type" header: "text/json". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".
将 Content-Type
更改为 application/json
会导致调用成功。