Google Sheets API - Python - Creating a Sheet - google-sheets-api

I am new to API's and i am trying to create a google sheet using python. I am able to create google sheet but unable to view/edit(Access issue). I am using Service Account in API's. Please help me in modifying the code to access the created sheets.
Here is the sample code i am using to create one.
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()

Most likely you are creating the sheet as the service account - by default on its Drive, this is why you have issues accessing it.
If instead you want to create a sheet on YOUR drive, you need to use impersonation:
After enabling domain-wide delegation, you need to impersonate the service account as you.
In python you can do it by specifying credentials = credentials.create_delegated(user_email) when instantiating the service object.

Related

How to fetch private data using Googlesheets API to my website using nodeJs

I need to access some data from a private Google Sheets document that only my google account has access to – it is not shared with anyone else.
From here: https://developers.google.com/sheets/guides/authorizing
When your application requests private data, the request must be
authorized by an authenticated user who has access to that data.
Again, that user would be me – the application developer. The users of my application will not have these sheets shared with them.
From what I’m reading in the Google API docs, I’m not sure this is possible – but it seems to me like it should be. I am using nodeJs.
Can anyone help me out?
You'll want to use Google's OAuth flow (here) in order to get access to private sheets. If only you need to be able to access the sheet then you can keep the OAuth as only tied to yourself and make requests to the endpoint in your app.
Example Implementation                                                                                
View in Fusebit
// Create a sheet object for the client + auth
const sheets = google.sheets({
version: 'v4',
auth
})
// Pull the data from our spreadsheet
const data = (await sheets.spreadsheets.values.get({
// See: https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms', // ID of the spreadsheet
range: 'Class Data!A2:E', // This gets all of the data in the sheet that we care about
})).data;
// Your rows/columns go into "data.values"
const rows = data.values;
});
Service account with domain-wide delegation:
The only way to avoid user interaction when accessing user private data is to use a service account with domain-wide delegation to impersonate your regular account.
That requires being a domain admin, though, so I'm not sure this option is available to you.
Sharing the spreadsheet with a service account:
Another option would be to share the spreadsheet with the service account, so you can access it using that account. Service accounts are not linked to a specific user, and so follow a different OAuth flow which doesn't require user interaction. See reference below.
Otherwise:
If you are not a domain admin and you don't want to share the spreadsheet with a service account, you are left with the web server workflow, which requires user interaction: you just cannot bypass this interaction.
I'd suggest you to read the references linked below.
Reference:
Using OAuth 2.0 for Web Server Applications
Using OAuth 2.0 for Server to Server Applications

Using Google Class room Java API in Tomcat Server as a rest API

I want to use Google Classroom Java API in Tomcat Server as a REST API.
I want the client-side code to generate an access token and refresh token and pass them to the REST API endpoints.
The REST API then use the above token to call the following code to get the list of Courses: objectOf(Classroom).courses().list().setPageSize(100).execute();
I am creating the Classroom as follows, where .getCredentials() uses
GoogleAuthorizationCodeFlow.Builder to create the credential required, but it opens a browser window to authorize the user and get the token. This doesn't work for me.
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Classroom classRoom = new Classroom.Builder(httpTransport, JSON_FACTORY, getCredentials(httpTransport))
.setApplicationName(APPLICATION_NAME).build();
I understand that you want to receive access and refresh tokens to later use them in Classroom; and you want to run this operation from a server. If that is correct, you would need to login as your account to prove your identity and receive the credentials, but there are some alternatives.
Since you are going to execute the code from a server you could follow these steps to create your credentials. Those credentials need to be saved in your work folder once and they can be read on every run.
Alternatively, you could create a service account and use it to reach your goals if you want to interact with Classroom as a different user of your organization. Please, remember to activate domain-wide delegation for this step. Don't hesitate to write back if you have some questions.

Calling Google Sheets API from Zoho Deluge function: The API Key and the authentication credential are from different projects

I am trying to read a Google Sheet by calling the Sheets API from a Zoho CRM Deluge function. I have created a project in the Google Developer Console and input the Key into the 'invokeurl' parameters. When I try to execute the call, I get the error:
"The API Key and the authentication credential are from different projects."
I tried creating an OAuth client ID, but the Google Sheets API only accepts Access Tokens. I admit, I don't know how to programmatically get an access token without giving consent with a pop up (not an option here).
I tried creating an access token using the Google OAuth Playground, but got the same "different projects" error.
I read on a Zoho support article that adding the headers:
{"X-HTTP-Method-Override":"PATCH","Content-Type":"application/json"}
help call Google APIs. When I did that, I get a different error:
"The requested URL /v4/spreadsheets/[spreadsheet ID]/values/[range]?key=[my key] was not found on this server. That’s all we know.
I have no other ideas. Any help would be greatly appreciated.
For reference, here is the relevant portion of the Zoho function:
params =
{
"key":myKey
};
sheet = invokeurl
[
url :"https://sheets.googleapis.com/v4/spreadsheets/" + id + "/values/" + range
type :GET
parameters:params
connection:"to_google_sheets"
headers: {"X-HTTP-Method-Override":"PATCH","Content-Type":"application/json"}
];
Turns out that since I had made a "connection" to the Google Sheets API in the Zoho CRM, I didn't need to pass the Key.
Removing that line made it work just fine.

Create an API to access google spreadsheet

I want to make a google spreadsheet available to a remote user. This user wants to use API requests to access the spreadsheet. What is the best way to implement?
Using Web app or using Execution API?
If the spreadsheet is not protected, you can easily access it using Spreadsheet's openById.
var ss = SpreadsheetApp.openById("abc1234567");
Logger.log(ss.getName());

BigQuery and GCM

I'm trying to create an Android app that extracts information from a table in BigQuery. In order to do this, do I have to have to implement Google Cloud Messaging? If so, do I have to have a GCM server? Is there a way, the app can authenticate directly with BigQuery and fire a query?
If you just want to make calls to BigQuery, you shouldn't need anything like Google Cloud Messaging (I'm not actually sure what that is).
You can either use the BigQuery java client (info here) or you can make raw http requests to BigQuery (you'll need to use Oauth2 for authorization).
You also might consider using an appengine app to proxy the requests to bigquery. That can make auth easier, so you don't need your BigQuery credentials in the android app.
I was trying to do the same.
I am able to do it in the Xamarin Studio(which uses C# API library of Google APIs).
Well..
First of all you need to authenticate the user..
The authentication is done using
private readonly static Google.Apis.Authentication.OAuth2.GoogleAuthenticator Auth = new Google.Apis.Authentication.OAuth2.GoogleAuthenticator (clientId,new Uri ("https://www.example.com/oauth2callback"), Google.Apis.Bigquery.v2.BigqueryService.Scopes.Bigquery.GetStringValue ());
Intent authIntent = Auth.GetUI (v.Context);
StartActivityForResult (authIntent, 1);//This will open and ask for Google account
So Auth is the Authorisation object.
var Service = new Google.Apis.Bigquery.v2.BigqueryService (Auth); //Creates the BigQuery Service
string query = "SELECT year, SUM(record_weight) as births FROM publicdata:samples.natality GROUP BY year";
Google.Apis.Bigquery.v2.JobsResource j = Service.Jobs;
Google.Apis.Bigquery.v2.Data.QueryRequest qr = new Google.Apis.Bigquery.v2.Data.QueryRequest ();
qr.Query = query;
Google.Apis.Bigquery.v2.Data.QueryResponse response = j.Query (qr, projectId).Fetch ();
Google.Apis.Bigquery.v2.Data.TableRow row = response.Rows.FirstOrDefault ();
Google.Apis.Bigquery.v2.Data.TableCell cell = row.F.FirstOrDefault ();
The above code is with respect Google APIs .NET library.
I am trying to use the Java API library.
I'll update my answer as soon as I am successful.
Hope the above answer gives you a starting point or an idea till then. :)