Xamarin Android with fitbit api- 将授权添加到标头



我有一个 xamarin android 应用程序,它通过他们的 API 和 xamarin.auth 连接到 fitbit,我有访问令牌,但在尝试获取 json 响应时收到未经授权的代码。我尝试将访问令牌添加到 url,但它不起作用。

这是我的代码:

if (e.IsAuthenticated)
{
var request = new OAuth2Request("GET", new System.Uri("https://api.fitbit.com/1/user/-/profile.json?access_token=" + e.Account.Properties["access_token"]), null, e.Account);
request.AccessTokenParameterName = e.Account.Properties["access_token"];
string type = e.Account.Properties["token_type"];
var response = await request.GetResponseAsync();
var json = response.GetResponseText();
}

我不知道如何将授权添加到OAuth2Request的标头中。

任何帮助都非常感谢!

因此,我最终添加了一个自定义请求类来扩展 OAuth2Request 类,并添加了另一个将访问令牌作为参数的 GetResponseAsync 函数。然后,我将授权添加到标头中,并保留函数的其余部分。不确定是否有办法使用 Xamarin.Auth 库做到这一点,但它对我有用。

public class CustomRequest : OAuth2Request{ //..
public virtual async Task<Response> GetResponseAsync(string accessToken){//..
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

最新更新