0
votes

http://skrinshoter.ru/s/260320/rW7vMcEV?a

I have an issue when I try to make append to my spreadsheet.

My Program.cs:

class Program
{
    private static GApiHandler gapi = GApiHandler.getInstance();
    static void Main(string[] args)
    {
        var db = new AppDbContext();
        var sheetInfo = db.SheetInfoes
            .Include(i => i.AMZ)
            .Include(i => i.Google)
            .Include(i => i.Template)
            .Include(i => i.Sheet)
            .Include(i => i.List)
            .Include(i => i.Operation)
            .FirstOrDefaultAsync().Result;
        //Console.WriteLine(JsonConvert.SerializeObject(sheetInfo));
        var row = new List<string>();
        row.Add("First Add");
        row.Add("Second column");
        row.Add("Third one");
        var append = new Append();
        append.values.Add(row);
        append.values.Add(row);
        append.values.Add(row);
        Console.WriteLine(JsonConvert.SerializeObject(append));
        gapi.Insert(sheetInfo, append);
        Console.WriteLine("Hello World!");
        Console.ReadKey();
    }
}      

Model of Append.cs:

public class Append
{
    public List<List<string>> values { get; set; }

    public Append()
    {
        values = new List<List<string>>();
    }
}    

Insert method that uses there:

public bool Insert(SheetInfo sheetInfo, Append append)
    {
        // tmp
        var spreadsheetId = sheetInfo.Sheet.Id;
        var range = "A2:Z5";
        var acc = sheetInfo.Google;
        //Console.WriteLine(values);
        var contentPairs = new[]
            {
                new KeyValuePair<string, string>("", JsonConvert.SerializeObject(append))
            };
        // end tmp
        var url = $"https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append?valueInputOption=ROW";
        Console.WriteLine(url);
        var response = RequestAsync(url, acc, contentPairs).Result;
        Console.WriteLine(response);

        return true;
    }       

RequestAsync method:

public async Task<string> RequestAsync(string url, GoogleAcc acc, IEnumerable<KeyValuePair<string, string>> contentPairs = null)
    {
        if (acc != null && acc.ExpiresAt < DateTime.Now)
            await NewToken(acc);
        using (var client = new HttpClient())
        {
            HttpResponseMessage result = null;
            FormUrlEncodedContent content = null;

            if (acc != null) client.DefaultRequestHeaders.Add("Authorization", $"Bearer {acc.AccessToken}");
            client.DefaultRequestHeaders.Add("Accept", "application/json");

            if (contentPairs != null)
            {
                content = new FormUrlEncodedContent(contentPairs);
                result = await client.PostAsync(url, content);
            }
            else
            {
                result = await client.GetAsync(url);
            }

            return await result.Content.ReadAsStringAsync();
        }
    }

The question is what query parameter 'names'??? There is no any query parameter 'names' specified by myself.

All works fine through 'TRY THIS API' when I specify 'spreadsheetId', 'range' and 'valueInputOption' - https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

Body for request:

{

"values": [ [ "First Add", "Second column", "Third one" ], [ "First Add", "Second column", "Third one" ] ] }

1

1 Answers

1
votes

var content = new StringContent(JsonConvert.SerializeObject(updateBatch));

It's a simple solution. I had to use StringContent except of FormUrlEncodedContent of course!

I found FormUrlEncodedContent there in StackOverflow as my VisualStudio IntelliSense do not found StringContent (or I miss key 'new')... and I start to think that there is no such class. Oh!.. Huge misstake!