我使用C#通用应用程序开发Dropbox API。
public async Task<string> Httpclient(string link) {
HttpClient client = new HttpClient();
HttpResponseMessage response= await client.GetAsync(link);
return await response.Content.ReadAsStringAsync();
}
public async Task<Model.Get_Current_Account.RootObject> get_current_account(string _accessToken) {
var query = await mainViewModel.Httpclient("https://api.dropboxapi.com/2/users/get_current_account?access_token="+_accessToken);
if (query != null) {
var get_data = JsonConvert.DeserializeObject<Model.Get_Current_Account.RootObject>(query);
return get_data;
} else
return null;
}
var query
产生错误:
调用API函数时出错&#34; users / get_current_account&#34;:必须提供HTTP标头&#34;授权&#34;或URL参数&#34;授权&#34;
答案 0 :(得分:1)
阅读保管箱API docs表示您应该传递授权标题(与您收到的错误相符)
Authorization: Bearer <access token>
this answer显示了如何向HttpClient添加标头
答案 1 :(得分:0)
不要使用HttpClient
,而是尝试使用HttpWebRequest
并设置API正在寻找的HTTP标头:
public async Task<string> Httpclient(string link, string token)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(link);
request.Accept = "application/json";
request.Headers.Add(
HttpRequestHeader.Authorization,
string.concat("Bearer ", token));
request.Method = "get";
var response = await request.GetResponseAsync();
var stream = response.GetResponseStream();
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
else return string.Empty;
}
catch { return string.Empty; }
}
这应该让你朝着正确的方向前进。
您需要提供token
数据才能获得成功授权。