Ceri Turner
Ceri Turner

Reputation: 946

Google API - Read from google sheets using API key, not OAuth2

I'm trying to move a project from Java over to c#. The project uses Google Sheets API key, not OAuth2

The documentation on Google's website show only how to do it for OAuth2 - https://developers.google.com/sheets/api/quickstart/dotnet#step_2_set_up_the_sample

The original Java code is

Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
            .setApplicationName(APPLICATION_NAME)
            .build();
Spreadsheet sp = service.spreadsheets().get(spreadsheetId).execute();
List<com.google.api.services.sheets.v4.model.Sheet> sheets = sp.getSheets();

and i'm struggling to convert to c#. I've tried

static string ApplicationName = "@Timetable";
private static readonly String API_KEY = "APIKEY";
static readonly string spreadsheetId = "SPREADSHEET ID";

    public void getSheets() {
    var service = new SheetsService(new BaseClientService.Initializer()
     {
        ApplicationName = ApplicationName,
        ApiKey = API_KEY,
     });
    var ssRequest = service.Spreadsheets.Get(spreadsheetId);
    Spreadsheet ss = ssRequest.Execute();
    List<string> sheetList = new List<string>();
}

but i get the error

System.ArgumentException: 'Invalid Application name Arg_ParamName_Name'

Whats the correct process?

Upvotes: 1

Views: 293

Answers (1)

Ceri Turner
Ceri Turner

Reputation: 946

Turns out it doesn't like spaces or special characters in the application name

Upvotes: 1

Related Questions