insert row to google spreadsheet using javascript post resuquest - javascript

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

You would need to authorize your postman application first using Google OAuth2 as referenced here in this article.
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.
Once you have enabled the API, click on Manage and proceed to the Credentials Tab to create your credentials.
Make sure that under Authorized Redirect URIs, put in "https://oauth.pstmn.io/v1/browser-callback"
Also, application type should be set to Web Application
Download the JSON file, and also make sure to save your Client ID and Client Secret.
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)
Set the client ID and client secret and use the scope "https://www.googleapis.com/auth/drive.file"
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.

Related

Unable to Publish a Post to Facebook Page Using Their API

I was trying to publish something to Facebook page.
From this article, it seemed really easy to publish using Facebook API
So, I create a Facebook page and made sure that I have all the required permission for the given auth token, opened by Postman and created a POST request to following URL (along with attaching my access token bearer)
https://graph.facebook.com/2984640844138/feed?message=HeyWhatever
This is giving me following error
{
"error": {
"message": "(#200) If posting to a group, requires app being installed in the group, and \\\n either publish_to_groups permission with user token, or both manage_pages \\\n and publish_pages permission with page token; If posting to a page, \\\n requires both manage_pages and publish_pages as an admin with \\\n sufficient administrative permission",
"type": "OAuthException",
"code": 200,
"fbtrace_id": "D1z1soQbTE2"
}
}
I am not sure about what I am doing wrong, perhaps my request is incorrect or I am not using postman correctly.
This is what I am doing in Postman (screenshot below), can someone point me out what I am doing wrong? Also, suggestions are also welcome
I would recommend you use the Facebook Graph API Explorer, which provides you with the tools to generate the access token with appropriate permission and also craft your HTTP requests with ease. Visit the same, generate a token, then head to the Access Token Debugger to double check the scopes of the token.

Javascript Youtube Data API v3 : Inserting video in a playlist (Video not found)

I am trying to add a video to my private playlist on youtube when I submit the form.
I request the id of the selected video, which also prints correctly in my console.
But when I try to add the video to the playlist, I get a 404 error : Video not found.However, if open up the error I get the following message...
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
This is my code to add the video to the playlist.
$('#form1').on('submit', function(e) {
e.preventDefault();
var id = $("#idArticle").val();
var request = gapi.client.youtube.playlistItems.insert({
part: 'snippet',
resource: {
snippet: {
playlistId: 'PLcQyg31odPUkASkMF09QBGqD8qKOQoe-l',
resourceId: {
videoId: id,
kind: "youtube#video",
}
}
}
});
request.execute(function(response) {
console.log(response);
$('#videoDetails').html('<p>' + response + '</p>');
});
console.log("Added.?");
});
Based from this thread, this problem may occur if you have either not authenticate your application using Oauth2 or you are not sending an API key along with this request.
You need a Google Account to access the Google Developers Console, request an API key, and register your application.
Create a project in the Google Developers Console and obtain authorization credentials so your application can submit API requests.
After creating your project, make sure the YouTube Data API is one of the services that your application is registered to use:
a. Go to the Developers Console and select the project that you just registered.
b. Open the API Library in the Google Developers Console. If prompted, select a project or create a new one. In the list of APIs, make sure the status is ON for the YouTube Data API v3.
If your application will use any API methods that require user authorization, read the authentication guide to learn how to implement OAuth 2.0 authorization.
see Getting started

Generate OAuth 2.0 Access Token For PUT Request In Google Sheets API In JavaScript

GOAL
I would like to update some information in a Google Sheets document though a web service. I don't mind if the data is public, but it seems like I need to get an OAuth token in order to use PUT https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}as opposed to simply getting the data. However, I get back errors stating that I'm missing OAuth 2.0 credentials.
EXPECTATIONS
In order to do this, I assume that I need to first get credentials from Google's Developer Console . I currently have an OAuth 2.0 client ID and API key through that console. The API key is used to read the data and the OAuth would be used to write data.
I then need to hit an endpoint of some kind with the OAuth information to get credentials that'll be used. I'm supposed to put this info into my PUT request. This is the documentation I've found in regards to my request, but it's missing the authorization documentation.
QUESTIONS
1) Where is the endpoint that I'm hitting to get a refresh token and access token in Google's OAuth 2.0 stuffs? I've heard it's something like https://accounts.google.com/o/oauth2/token, but I'm not sure how to get the tokens. What do I need to pass? How do I pass that info? I can't find any documentation over it.
2) After getting these tokens, how do I incorporate them into the PUT request to the Google Sheets API? Do the tokens go into the Authorization header or the access_token query parameter or something?
I would highly recommend to check the Javascript Quickstart for Sheets API first as it includes OAuth sign-in demo.
From the quickstart code, this is the part that calls the OAuth library:
function handleClientLoad() {
gapi.load('client:auth2', initClient);
}
And here's me using the PUT request in an app I made:
function recordTimeInSheet(){
var params = {
"range":"Sheet1!"+A+":"+C,
"majorDimension": "ROWS",
"values": [
["bruceWayne","8:00AM", "5:00PM"]
],
}
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://sheets.googleapis.com/v4/spreadsheets/'+myspreadsheetId+'/'+"values/"+"Sheet1!"+A+":"+C+"?"+'valueInputOption=USER_ENTERED');
xhr.setRequestHeader('Authorization', 'Bearer ' + myAccessToken);
xhr.send(JSON.stringify(params));
}

Google Drive API V3: Creating file permissions for a domain

Trying to add permissions to a file via Google Drive's API V3 and I ran into the error below. I want to allow requests from mta.io, my site, to able to read the file. The error seems to come from what domain I pass in the body of request for example, example.com works fine and permissions are granted to it. Do I need to whitelist my domain in order to give it permissions to the file?
Works:
{
"role": "reader",
"type": "domain",
"domain": "example.com"
}
Doesn't work:
{
"role": "reader",
"type": "domain",
"domain": "mta.io"
}
Error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "The specified domain is invalid or not applicable for the given permission type.",
"locationType": "other",
"location": "permission.domain"
}
],
"code": 400,
"message": "The specified domain is invalid or not applicable for the given permission type."
}
}
I'm using the try it feature found on the API's site.
Figured it out, you can only use G Suite domains.
It is a bummer but in order to share file permission exclusively with a domain you need to have a G Suite account and verify that you own that domain - the domain needs to be linked with your G Suite account.
https://developers.google.com/drive/v3/web/about-permissions#types_and_values
For example, a permission with a type of domain may have a domain of thecompany.com, indicating that the permission grants the given role to all users in the G Suite domain thecompany.com
Based from this related thread, this can only be done between users in the same domain, and service accounts don't belong to any domain.
You're best option may be to create a Team Drive that the service account has access to, and perform a two stage process:
Use the service account to move the file into the team drive. Files.update with the addParents parameter set to the Team Drive ID.
Use the domain user to move the file out of the team drive. Files.update with the addParents parameter set to root (or some target folder's ID) and the removeParents parameter set to the Team Drive ID.
Here's another SO post which might also help: Google Drive SDK - change item sharing permissions

Office 365 apis failing with a 401

I am trying to integrate office 365 apis with my application. I am able to generate the access token using the OAuth2 process. But calling the api with that generated access token is giving us a 401 error.
This is the response of token I am getting
{
"token_type": "Bearer",
"scope": "ActivityReports.Read ThreatIntelligence.Read",
"expires_in": "3599",
"ext_expires_in": "0",
"expires_on": "1480350031",
"not_before": "1480346131",
"resource": "https://manage.office.com",
"access_token": "===access token===",
"refresh_token": "===refresh token===",
"id_token": ""
}
And I am trying to call the endpoint "https://outlook.office.com/api/v2.0/me" with the header as Authorization: Bearer , but giving me the 401 error.
Any thoughts ?
Thanks,
I believe the issue here is that you have acquired an access token for an endpoint that is different than the API you are trying to query.
If you notice, the "resource" in your token is "manage.office.com" when you will need a token with the resource "outlook.office.com" in order to query the outlook API.
Can you check in your code that you are specifying the correct resource for your needs, and additionally, make sure your client application is configured correctly for the permissions required to access that API.
I hope this helps!

Categories

Resources