Bolisov / google-api-typings-generator

⚠️ This repo is no longer maintained and outdated, use active fork ⚠️
https://github.com/Maxim-Mazurok/google-api-typings-generator
8 stars 5 forks source link

gapi.client.spreadsheets.get() TypeError: Cannot read property 'get' of undefined #2

Open lookingwestapps opened 7 years ago

lookingwestapps commented 7 years ago

First of all, thanks for making this generator!

I'm having a problem using types/gapi.client.sheets.

When I call, await gapi.client.spreadsheets.get({ spreadsheetId: "spreadsheetId" }); I get an error saying: "TypeError: Cannot read property 'get' of undefined"

Implementation

My Angular 4 app successfully loads the "client" and then "sheets" like the example from the readme (with a few of my own additions):

    gapi.load("client", () => {
        // now we can use gapi.client

        // Set API KEY
        gapi.client.setApiKey('xxxxxxxxxxxxxx');

        gapi.client.load('sheets', 'v4', () => {
          // now we are supposed to be able to use gapi.client.sheets 
          this.authenticateClient();
        });
    });

I then successfully authorize the client just like the example (with a few additions of my own):

  authenticateClient(immediate: boolean = true): void {
    // declare client_id registered in Google Developers Console
    var client_id = 'xxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
        scope = [
             // View your Google Spreadsheets
            'https://www.googleapis.com/auth/spreadsheets.readonly',
        ];

    gapi.auth.authorize({ client_id: client_id, scope: scope, immediate: immediate }, authResult => {
        if (authResult && !authResult.error) {
            /* handle successful authorization */
            console.log("GAPI IS AUTHORIZED!!!", authResult);
        } else {
            /* handle authorization error */
            if (authResult.error == "immediate_failed") {
              // the immediate: true authorization failed, allow the user to try
              // again by clicking signin button to call with immediate = false
              console.log("Immediate:True authorization failed... need to sign-in");
              // this.authenticateClient(false); // If you try to do this automatically, the browser will block the pop-up
            } else {
              console.log("ERROR authorizing GAPI! error:", authResult);
            }
        }
    });
  }

My index.html loads the javascript with this line: <script async defer src="https://apis.google.com/js/api.js"></script> Also I added "gapi.client.sheets" to my "types" in tsconfig.app.json.

ERROR

All that works... after authorizing succeeds, when I click a button that calls, await gapi.client.spreadsheets.get({ spreadsheetId: "spreadsheetId" }); I get an error saying: "TypeError: Cannot read property 'get' of undefined" So for some reason .spreadsheets is undefined.

If I set a breakpoint in the browser I can examine the gapi.client to see it does NOT have a spreadsheets property, but it DOES have a sheets property and that has a spreadsheets property on it. So gapi.client.sheets.spreadsheets.get() exists, but gapi.client.spreadsheets is undefined.

client spreadsheets get undefined

I can workaround this issue by adding a "sheets" variable inside of types/gapi.client.sheets/index.d.ts on line 20 adding: const sheets: any;

And then calling the spreadsheets.get() function like this:

await gapi.client.sheets.spreadsheets.get({ spreadsheetId: "1BxiMV...." }.then(console.log)
CONSOLE: {result: {…}, body: "{↵  "spreadsheetId": "1BxiMV..../edit"↵}↵", headers: {…}, status: 200, statusText: null}

What am I doing wrong that my gapi.client.spreadsheets is undefined?

Bolisov commented 7 years ago

Hi,

Thank you for your feedback. I posted pull request to DT repo - https://github.com/DefinitelyTyped/DefinitelyTyped/pull/21303 to fix this issue

nikyoudale commented 6 years ago

It would be great to see this fixed 😊

amoshydra commented 5 years ago

Unfortunately, the PR got closed https://github.com/DefinitelyTyped/DefinitelyTyped/pull/21303#issuecomment-355133151

There are 5 additional open issues about this #3 #4 #5 #6 #7

alexturpin commented 4 years ago

@Bolisov any chance you could make another PR on DT for this? As it stands the actual type declarations are pretty much unusable :(

namoscato commented 4 years ago

I stumbled across this issue and documented at least one of the issues with current (stale) state in https://github.com/Bolisov/google-api-typings-generator/issues/13 before realizing there are more active forks.

@Maxim-Mazurok and @declanvong, have you chatted about the future of this library and/or started incorporating your functional forks back into DefinitelyTyped?

Maxim-Mazurok commented 4 years ago

I stumbled across this issue and documented at least one of the issues with current (stale) state in #13 before realizing there are more active forks

I'm glad you're also interested in this project! Indeed, we have active fork, and we decided to use mine as the main one. So, feel free to open PRs. Regarding your issue, I've tested and submitted updated version of Google sheets API to Definitely Typed recently. Can you check if it'll work for you? I'll look more into this issue later, probably after holidays :)

@Maxim-Mazurok and @declanvong, have you chatted about the future of this library and/or started incorporating your functional forks back into DefinitelyTyped?

Yes, we've had some thoughts about the future of it. From the top of my head, we want to add prettier to make collaboration easier. Also, we want to drop all "pretty" stuff from generator and just reformat it after all code is generated. Otherwise, we'll be creating our own prettier, which is obviously out of scope of the project. I also think it would be great to start regularly update typings, as APIs keep changing and automatically submit them to Definitely Typed. But it's kind of a long term goal. Also, we need more people that will test all of the different APIs. Currently I'm working with Sheets for the most part. And @declanvong uses Classroom API.

Feel free to contact me on social, all links are on my website: https://maxim.mazurok.com

namoscato commented 4 years ago

I've tested and submitted updated version of Google sheets API to Definitely Typed recently. Can you check if it'll work for you?

Yeah, it looks like this issue and #13 were fixed with https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37847, and I see you updated things again in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/41155. Things look fine from my perspective – thanks! It would be nice to close out this issue for clarity.

Yes, we've had some thoughts about the future of it.

I would be happy to help out but will continue to the conversation elsewhere!

viviakemik commented 4 years ago

Hi, I have this same issue with gapi.client.people.get() TypeError: Cannot read property 'get' of undefined. When I logged "gapi.client.people" the result is Untitled

How can I resolve this? (I think the structure is incorrect "gapi.client.people.people.get" instead of "gapi.client.people.get" ) Thanks!

Maxim-Mazurok commented 4 years ago

@viviakemik indeed, it seems like you have to use it like this:

/**
 * Provides information about a person by specifying a resource name. Use
 * `people/me` to indicate the authenticated user.
 *
 * The request throws a 400 error if 'personFields' is not specified.
 */
await gapi.client.people.people.get({
    personFields: "Test string",
    "requestMask.includeField": "Test string",
    resourceName: "Test string",
});

Please, open new issue here if you run into any issues with using gapi.client.people typings. I can update gapi.client.people typings if they are outdated. Also, you can use fresh typings from our types branch which is updated every hour. Thanks!

Maxim-Mazurok commented 4 years ago

This repo is abandoned, see #19 and use active fork instead: Maxim-Mazurok/google-api-typings-generator

Please, close this issue if you will, thanks for your cooperation!