Google API批量请求 - .NET REST调用给出错误

时间:2017-11-08 09:19:15

标签: rest google-api restsharp

我正在使用.NET代码在批量请求中调用google api来获取多个ID的消息详细信息。我正在创建的请求,当通过postman运行它完美无缺但运行此控制台应用程序时它会给我Response Status code = 0和错误Specified value has invalid CRLF characters.\r\nParameter name: value

有人可以看看这个并帮助我......我现在几乎搜索了所有内容,但无法得到任何解决方案。

这是我的代码 -

            var baseUrl = "/gmail/v1/users/" + _accountID + "/messages/";

            RestClient client = null;
            RestRequest request = null;
            StringBuilder sbBody = null;
            for (int j = 0; j < _messageId.Count; j++)
            {
                msgCount++;
                if (msgCount == 1)
                {
                    boundary = "testing_batch";
                    sbBody = new StringBuilder();
                    client = new RestClient("https://www.googleapis.com/batch/");
                    request = new RestRequest(Method.POST);

                    request.AddHeader("Content-Type", "multipart/mixed; boundary=\"" + boundary + "\"");
                    request.AddHeader("Authorization", "Bearer " + accessToken);
                }

                sbBody.Append(string.Format("--{0}\n", boundary));
                sbBody.Append("Content-Type: application/http\n\n");
                sbBody.Append(string.Format("GET {0}{1}\n\n", baseUrl, _messageId[j]));

                if (msgCount == 1)
                    sbBody.Append(string.Format("--{0}--", boundary));

                if (msgCount == 1)
                {
                    msgCount = 0;
                    //request.AddBody(sbBody.ToString());
                    request.AddHeader("RequestBody", sbBody.ToString());
                    IRestResponse response = client.Execute(request);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {

                            //Do nothing for now

                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        Console.WriteLine(response.StatusCode);
                        Console.WriteLine(response.Content);
                        Console.ReadLine();
                        break;
                    }
                    else
                    {
                        Console.WriteLine(response.StatusCode);
                        Console.WriteLine(response.Content);
                        Console.ReadLine();
                        break;
                    }
                }

0 个答案:

没有答案