Hichem Om
Hichem Om

Reputation: 21

insert row to google spreadsheet using javascript post resuquest

i am trying to insert a new row to my google spreadsheet using JS from broser console using post man made a post request to this url

https://sheets.googleapis.com/v4/spreadsheets/1KjrAqsNzzNIyEDb4hjx846j2AVffSeLUqi0BMR_pKPM/values/Sheet1:append?key=AIzaSyAc5XWsjlFeR3omZiYnrzaEAbZAYIWUXqI

injecting this json:

{
            "asin": "aaaa",
            "price": "15.99",
            "title": "ratings",
            "bsr" : "555523",
            "image_url":"hhhhhhh"
        }

and i got this error:

{
    "error": {
        "code": 401,
        "message": "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See https://cloud.google.com/docs/authentication",
        "status": "UNAUTHENTICATED",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.ErrorInfo",
                "reason": "CREDENTIALS_MISSING",
                "domain": "googleapis.com",
                "metadata": {
                    "service": "sheets.googleapis.com",
                    "method": "google.apps.sheets.v4.SpreadsheetsService.AppendValues"
                }
            }
        ]
    }
}

my spreadsheet has 4 columns:

asin    title   bsr price   image_url

Upvotes: 1

Views: 1665

Answers (1)

Century Tuna
Century Tuna

Reputation: 1762

You would need to authorize your postman application first using Google OAuth2 as referenced here in this article.

  1. You would need to create a Google Cloud Project on console.cloud.google.com, and under API's and Services > Enabled API's and Services, look for Google Sheets API.
  2. Once you have enabled the API, click on Manage and proceed to the Credentials Tab to create your credentials.
  3. Make sure that under Authorized Redirect URIs, put in "https://oauth.pstmn.io/v1/browser-callback"
  4. Also, application type should be set to Web Application
  5. Download the JSON file, and also make sure to save your Client ID and Client Secret.
  6. From the downloaded JSON file, you would need to save the "auth_uri" and "token_uri" (you'll be needing this for authorization on your postman console for auth url and access token url)
  7. Set the client ID and client secret and use the scope "https://www.googleapis.com/auth/drive.file"
  8. From there, once you have generated your access token, make your post request following the sample here in this article: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append. YOUR_API_KEY should be the access token that was generated earlier on your Postman console.

If ever you experience other issues with authorizing your account for OAuth2, proceed with going to the OAuth Consent Screen on your Google Cloud Console, create an app, and define the said scope above under the Non-sensitive scopes, and add your Google Account under Test Users.

Upvotes: 0

Related Questions