【问题标题】:Google Sheets OAuth2Request - Bad Request in Xamarin FormsGoogle Sheets OAuth2Request - Xamarin 表单中的错误请求
【发布时间】:2020-03-04 21:57:21
【问题描述】:

由于 Google Sheets Api 不支持 Xamarin,我正在尝试使用 Xamarin.Auth.OAuth2Request 构建请求。

以下代码表示请求的样子:

Dictionary<string, string> value = new Dictionary<string, string> { { "range", "Arkusz1!A1:B1" },{ "values", "[Test,123]" }};

var request = new OAuth2Request("POST", new Uri("https://sheets.googleapis.com/v4/spreadsheets/13FC0GZPbFbbOk8MW3N4fRvYq3lNKNZqsQdRsi0uVmgQ/values/Arkusz1!A1:B1:append?valueInputOption=RAW"), value, GoogleAccount);
var response = request.GetResponseAsync().Result;

GoogleAccount 是在登录时创建的 Account 对象。

不幸的是,我收到了回复:“Bad Request”:Screenshot from debugger

我认为问题出在 OAuth2Request 的第三个参数(IDictionary 参数),但我不知道如何检查它。 我怎样才能获得更多信息是什么导致了这种反应?

【问题讨论】:

    标签: xamarin oauth-2.0 google-sheets-api


    【解决方案1】:

    我已经尝试了所有在 OAuth2Request 中传递参数的方法,但它仍然不想合作。 最后为了处理我的请求,我使用了 HttpClient 和 HttpContent

    以下代码代表我的最终工作算法:

    //Initialize HttpClient instance
    HttpClient client = new HttpClient();
    
    //Dictionary whose contents correlates to body of request
    Dictionary<string, List<string[]>> values = new Dictionary<string, List<string[]>>();
    values.Add("values",new List<string[]>() { new string[] { "Test", "123" } });
    
    //Serialize Dictionary to JSON
    var json = JsonConvert.SerializeObject(values);
    
    //Create StringContent which is based on JSON
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    
    //Add authentication header. It can be taken from Account object
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",GoogleAccount.Properties["access_token"]);
    
    //Call Post Request with our new content. Run it asynchronously or not.
    var output = client.PostAsync(new Uri(@"https://sheets.googleapis.com/v4/spreadsheets/13FC0GZPbFbbOk8MW3N4fRvYq3lNKNZqsQdRsi0uVmgQ/values/Arkusz1!A1:B1:append?valueInputOption=RAW"), content).Result;
    
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多