调用API函数时出错" users / get_current_account":必须提供HTTP标头"授权"或URL参数"授权"

时间:2016-02-09 07:51:33

标签: c# win-universal-app dropbox-api

我使用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;

2 个答案:

答案 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数据才能获得成功授权。