How to connect Google sheet API on AWS EC2 - python

I am trying to use Google Sheets API to load data into EC2 using Python.
So, I tried this quickstart.
But I am stuck configuring OAuth client to get credential.json file. I can't understand what drop-down type I should select.
Hope I was clear. Thanks in advance for your time.

Depending on the type of applications you want to create, you will have to choose one of the options provided in the dropdown.
Since you want to use a Python script, you can use the credentials of type Desktop and connect to AWS EC2 from the same application. However, you can always create new ones in the corresponding GCP project to match the application you are working on.
Reference
Sheets API Authorize Requests.

Related

Azure api or sdk to get list of app registrations and the certificates associated with them

I want to connect my python application to azure.
Problem statement:
Get a list of apps registered with their certificate thumbprints and expiry details.
I tried a lot but can't find any APIs. Please help me.
You will need to use Microsoft Graph API for that.
The API operation you would want to use is List applications. The information about the certificates associated with each application will be available in keyCredentials attribute in the response.
You could use the Azure SDK for this. For Authorization check out the Identity Package.
Do you want to host your app in Azure as well? Then you should use Manged Identity. Alternatively, create a service principal for your app. This must get the read rights to the app registrations.
If you use the SDK, you only need a credential object that is created with Tenant ID, App Id and Secret (Service Principal). See here: https://learn.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate
Basically I recommend you to use the SDK instead of calling the GRAPH. The SDK takes a lot of work from you ;)

User authentication for Spotify in Python using Spotipy on AWS

I am currently building a web-app that requires a Spotify user to login using their credentials in order to access their playlists
I'm using the Spotipy python wrapper for Spotify's Web API and generating an access token using,
token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri)
The code runs without any issues on my local machine. But, when I deploy the web-app on AWS, it does not proceed to the redirected uri and allow for user login.
I have tried transferring the ".cache-username" file via SCP to my AWS machine instance and gotten it to work in limited fashion.
Is there a solution to this issue? I'm fairly new to AWS and hence don't have much to go on or any idea where to look. Any help would be greatly appreciated. Thanks in advance!!
The quick way
Run the script locally so the user can sign in once
In the local project folder, you will find a file .cache-{userid}
Copy this file to your project folder on AWS
It should work
The database way
There is currently an open feature request on Github that suggests to store tokens in a DB. Feel free to subscribe to the issue or to contribute https://github.com/plamere/spotipy/issues/51
It's also possible to write a bit of code to persist new tokens into a DB and then read from it. That's what I'm doing as part of an AWS Lambda using DynamoDB, it's not very nice but it works perfectly https://github.com/resident-archive/resident-archive/blob/a869b73f1f64538343be1604d43693b6165cc58a/functions/to-spotify/main.py#L129..L157
The API way
This is probably the best way, as it allows multiple users to sign in simultaneously. However it is a bit more complex and requires you host a server that's accessible by URL.
This example uses Flask but one could adapt it to Django for example https://github.com/plamere/spotipy/blob/master/examples/app.py

How do I use GAE to listen to Google sheets and then run a set of python code?

Hey all I am looking to use GAE to listen to one of my Google sheets pages and then execute a python script based of what was entered. I have already setup the python and sheets interaction, meaning I can now read everything off my sheet when I run my code. I am having trouble understanding how to setup GAE to "listen" for changes an the sheets though. I have setup the GAE using:
https://cloud.google.com/appengine/docs/standard/python3/quickstart
But not sure how to integrate this using my sheets. Any help would be great!
What you want is doable but not in the same way you described. GAE cannot listen to Google Sheets. The usually interaction is with the Google Sheets API, which will require you firing off API calls to check for a certain sheet/cell for updates.
Since you are looking for a "push" solution, you will need to do it from Google Sheets' end. You need Google Sheets to fire off a request to your GAE and then you can process the information. I would suggest you code your GAE handler to process GET requests and then have your Google Sheets fire off a script to set a request to your GAE URL. You will set the script to trigger whenever the desired update on your sheet is performed.

How can I post some data from google app script to my python script on the local machine?

I have a google spreadsheet and a google app script which allows me to get the user selected values on the sheet. I need to know a way which helps me to send me this user selection to my python script on the local machine so that I query my data storage file and return back the updated values to the google sheet.
I have tried using python quickstart but this doesnt let me get the data selected by the user on the spreadsheet.
I have tried to use Socket.io to establish a connection between google script and flask server but havent been successful at it.
Any pointers would be greatly appreciated.
You could capture the selected information and store it in a file and Google Drive Sync will copy it to your local drive where you could open it with Python.

GAE: Can't Use Google Server Side API's (Whitelisting Issue)

To use Google API's, after activating them from the Google Developers Console, one needs to generate credentials. In my case, I have a backend that is supposed to consume the API server side. For this purpose, there is an option to generate what the Google page calls "Key for server applications". So far so good.
The problem is that in order to generate the key, one has to mention IP addresses of servers that would be whitelisted. But GAE has no static IP address that I could use there.
There is an option to manually get the IP's by executing:
dig -t TXT _netblocks.google.com #ns1.google.com
However there is no guarantee that the list is static (further more, it is known to change from time to time), and there is no programatic way I could automate the use of adding IP's that I get from dig into the Google Developers Console.
This leaves me with two choices:
Forget about GAE for this project, ironically, GAE cannot be used as a backend for Google API's (better use Amazon or some other solution for that). or
Program something like a watchdog over the output of the dig command that would notify me if there's a change, and then I would manually update the whitelist (no way I am going to do this - too dangerous), or allow all IP's to use the Google API granted it has my API key. Not the most secure solution but it works.
Is there any other workaround? Can it be that GAE does not support consuming Google API's server side?
You can use App Identity to access Google's API from AppEngine. See: https://developers.google.com/appengine/docs/python/appidentity/. If you setup your app using the cloud console, it should have already added your app's identity with permission to your project, but you can always check that out. From the "Permissions" Tab in cloud console for your project, make sure your service account is added under "Service Accounts" (in the form of your_app_id#appspot.gserviceaccount.com)
Furthermore, if you use something like the JSON API Libs available for python, you can use the bundled oauth2 library to do all of this for you using AppAssertionCredentials to authorize the API you wish to use. See: https://developers.google.com/api-client-library/python/guide/google_app_engine#ServiceAccounts
Yes, you should use App Identity. Forget about getting an IP or giving up on GAE :-) Here is an example of how to use Big Query, for example, inside a GAE application:
static {
// initializes Big Query
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new UrlFetchTransport();
AppIdentityCredential credential = new AppIdentityCredential(Arrays.asList(Constants.BIGQUERY_SCOPE));
bigquery = new Bigquery.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(Constants.APPLICATION_NAME).setHttpRequestInitializer(credential)
.setBigqueryRequestInitializer(new BigqueryRequestInitializer(Constants.API_KEY)).build();
}

Categories

Resources