Google sheet API token expiration Python - python

I'm currently working on a private script that allows for data warehouse ETL work to run. I'm saving my env secrets on a cloud vault. The problem comes when a script runs and pulls information from a private google sheet, this requires a token generated from a project on G Cloud, however, it appears the token has a limited lifespan, is there a way to get a perma-token that does not expire? (Without publishing the project)
Thanks

The issue is that you haven't published the project, it will expire every 7 days.
To solve the issue you will need to publish the project (you can leave it as private) by following the steps:
1. Access to console.cloud.google.com
2. Make sure that you have selected the correct project.
3. Click the 3 lines for the menu.
4. Search for "Marketplace."
5. At the top, in the search bar, type "Google Workspace Marketplace SDK," and press enter.
6. On the Google Workspace Marketplace SDK page, click Enable.
7. Create the store listing

Related

How to get phonemes from Google Cloud API Text-to-Speech

I am following the Google Cloud API Text-to-Speech Python tutorial. I would like to know if there is a way to return the phonemes and their duration, an intermediate step in generating the interpreted speech. Is that possible? If so, can you please refer me to the documentation and hopefully some sample code that does this. I searched and could not find anything that already answered my question.
Thanks!
gma
Mentioning all the steps to get phonemes from Google cloud API Text-to-Speech. In Part-3, you can find the sample code.
Here are the steps you can follow:
[Part-1]
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Cloud project
Enable the Cloud Text-to-Speech API.
Create a service account:
a. In the Cloud Console, go to the Create service account page.
b. Select a project.
c. In the Service account name field, enter a name. The Cloud Console fills in
the Service account ID field based on this name.
d. Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
Create a service account key:
a. In the Cloud Console, click the email address for the service account
that you created.
b. Click Keys.
c. Click Add key, then click Create new key.
d. Click Create. A JSON key file is downloaded to your computer.
e. Click Close.
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.
Example 1. Linux or macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
Replace KEY_PATH with the path of the JSON file that contains your service account key.
For example:-
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Example 2. Windows
For powershell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
Replace KEY_PATH with the path of the JSON file that contains your service
account key.
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
For command promt:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
Replace KEY_PATH with the path of the JSON file that contains your service
account key.
Install and initialize the cloud SDK.
[Part-2]
Install the client library
pip install --upgrade google-cloud-texttospeech
[Part-3]
Create audio data
Now you can use Text-to-Speech to create an audio file of synthetic human speech. Use the following code to send a synthesize request to the Text-to-Speech API.
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
If you face any issue, please refer to the link below:
https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-install-python
Thanks for your reply #Akshansha.
I know how to create an audio file of synthetic human speech. My question was more about how to get metadata like phoneme or viseme. For exemple, with the Amazon Polly API you can get this kind of data when using Text-to-Speech :
{"time":0,"type":"sentence","start":0,"end":23,"value":"Mary had a little lamb."}
{"time":6,"type":"word","start":0,"end":4,"value":"Mary"}
{"time":6,"type":"viseme","value":"p"}
{"time":73,"type":"viseme","value":"E"}
{"time":180,"type":"viseme","value":"r"}
{"time":292,"type":"viseme","value":"i"}
{"time":373,"type":"word","start":5,"end":8,"value":"had"}
{"time":373,"type":"viseme","value":"k"}
{"time":460,"type":"viseme","value":"a"}
{"time":521,"type":"viseme","value":"t"}
{"time":604,"type":"word","start":9,"end":10,"value":"a"}
{"time":604,"type":"viseme","value":"#"}
{"time":643,"type":"word","start":11,"end":17,"value":"little"}
{"time":643,"type":"viseme","value":"t"}
{"time":739,"type":"viseme","value":"i"}
{"time":769,"type":"viseme","value":"t"}
{"time":799,"type":"viseme","value":"t"}
{"time":882,"type":"word","start":18,"end":22,"value":"lamb"}
{"time":882,"type":"viseme","value":"t"}
{"time":964,"type":"viseme","value":"a"}
{"time":1082,"type":"viseme","value":"p"}
I was asking if we can have a similar result with the Google Cloud API TTS ?
Thanks,
gma

Update google sheet without Oauth or API key

Basically I am looking for updating the google sheet from my linux/unix box using shell scripts. Since I couldnt find much with shell scripts , thought of using python to update the google sheet.
Most answers were redirecting to create google project for its api key / OAuth key. I could not create a project in google api / generate a api key as i dont have access to create one at organization level / no organization level. Its asking for location to choose and I dont find any.
Is there any other way to update a google sheet with wget or sudo or CURL or any other method in python ?
You actually don't need an organization to create an API Key, when you click on "New Project" in the Google Cloud Platform you can leave it blank.
https://i.stack.imgur.com/m7C2T.png
Then you have to use the api key in your python code to be able to access your sheets.

How can one write to a publicly available Google sheet (without authorization) in Python?

I have full edit access to a Google Sheet not owned by me. I want to be able to write to the spreadsheet using Python without Google API authorization. I checked several available packages (gdata, gspread etc.) and seems all of them ask for the credentials.
I was also able to read the content of a spreadsheet without authorization using requests or pd.read_csv() by pandas (I tweaked the URL by changing the last part saying ...edit#gid=... to ...export?format=csv&gid=...). Yet, when sending a POST request to the same URL I received 200 status code but the same old empty spreadsheet.
Any help is highly appreciated.
I believe your goal as follows.
You want to put the values to the publicly shared Google Spreadsheet using python.
In this case, you want to access to the Spreadsheet without authorization.
For this, how about this answer?
Issue and workaround:
In order to put the values to the publicly shared Google Spreadsheet, the POST method is used. In this case, it is required to use the access token. On the other hand, in the case of the GET method, when the Sheets API is used, an API key can be used. And the endpoints like exportLinks, you can retrieve the values without the API key. These are the specification of Google side.
Under this condition, in order to achieve your goal, I would like to propose the following 2 patterns.
Pattern 1:
In this pattern, I would like to propose to access to the Spreadsheet using the access token retrieved from the service account. In this case, the script can be simpler.
Sample script:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
spreadsheetId = "###" # Please set the Spreadsheet ID.
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.sheet1
worksheet.update_acell('A1', 'sample')
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet using gspread.
Pattern 2:
In this pattern, I would like to propose to access to the Spreadsheet using the Web Apps created by Google Apps Script as the wrapper API. In this case, the python script is more simpler.
Usage:
Please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Sheets API at Advanced Google services. This script is for the Web Apps.
function doPost(e) {
try {
const spreadsheetId = e.parameter.spreadsheetId;
const obj = JSON.parse(e.postData.contents);
const resource = obj.body;
const range = obj.arguments.range;
const valueInputOption = obj.arguments.valueInputOption;
Sheets.Spreadsheets.Values.update(resource, spreadsheetId, range, {valueInputOption: valueInputOption});
return ContentService.createTextOutput("ok");
} catch(e) {
return ContentService.createTextOutput(JSON.stringify(e));
}
}
In this case, the POST method is used.
In this sample script, as a test script, the values are put to the Spreadsheet using the method of spreadsheets.values.update in Sheets API.
3. Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
By this, the script is run as the owner.
Select "Anyone, even anonymous" for "Who has access to the app:".
In this case, no access token is required to be request. I think that I recommend this setting for your goal.
Of course, you can also use the access token. At that time, please set this to "Anyone".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Run the function using Web Apps.
This is a sample python script for requesting Web Apps. Please set your Web Apps URL, Spreadsheet ID and range.
import json
import requests
spreadsheet_id = '###' # Please set the Spreadsheet ID.
body = {
"arguments": {"range": "Sheet1!A1", "valueInputOption": "USER_ENTERED"},
"body": {"values": [["sample"]]}
}
url = 'https://script.google.com/macros/s/###/exec?spreadsheetId=' + spreadsheet_id
res = requests.post(url, json.dumps(body), headers={'Content-Type': 'application/json'})
print(res.text)
In this sample script, sample is put to the cell "A1" of the 1st tab in the publicly shared Spreadsheet.
In this case, no authorization is required in the python script, because it has already been done when Web Apps is deployed.
Note:
When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
Advanced Google services
publish a Google Spreadsheet through Google Apps Scripts
spreadsheets.values.update

Retrieving Facebook Page comments with python script

I am building a python script that is trying to stream a screen capture to my Facebook Page and to be able to retrieve all the comments from the Facebook Live stream real time so that I can do some processing in the middle of the stream.
The Facebook App was set up (in development mode) but when I tried to retrieve the comments from my live stream, I am only able to retrieve comments with their name and id ("from") that are made as the Facebook Page Admin, not comments that are made by other users. I need the user's id, user's name and their comments.
I understand that I need to get Facebook App to be live mode in order to retrieve all the comments with their details tagged to it. When I tried to get it, it tells me that I need to get the permission approved. I tried to fill in most of the stuff and try to get the two permission (manage_page for the comments and live video API for the streaming) but I was unable to because I left the platform empty.
Below is the message I got:
You do not have any platforms eligible for review. Please configure a platform on your Settings page.
The problem is when I tried to choose a platform that was shown in the list, python script does not fall in the list of platform.
Does anyone know of a solution or a different way to achieve what I need to retrieve?
Have you tried using PyLivestream?
It can be used to stream to Facebook Live using FFmpeg (to multiple services simultaneously actually, like Periscope, YouTube etc).
It adheres to the RTMPS requirement and should be an option for you if I interpret your needs correctly.
python -m pip install PyLivestream
Facebook Live
Facebook Live requires FFmpeg >= 4.2 due to mandatory RTMPS
configure your Facebook Live stream
Put stream ID from https://www.facebook.com/live/create into the file facebook.key
Run Python script for Facebook with chosen input
Check out the PyPi PyLivestream page for details.
To be able to retrieve all the comments from the Facebook Live stream
I'm not sure if this is possible using PyLivestream alone, but the Polls API can be used to represent VideoPoll objects in the Graph API, to create polls on live video broadcasts and get real-time responses from your viewers and can be created with the
POST /{live-video-id}/polls
endpoint on a LiveVideo object.
Upon creation, the API will return a VideoPoll object ID, which you can use to manipulate the poll and query for viewer interactions.
Guess you'll have to do a bit of digging to figure out the details,
but I believe this would be the right way to approach this task.
In order to get the "from" field when retrieving the comments, you need to have manage_pages permission from your Facebook App that is linked to your Facebook Page. You will need to submit an App review for your Facebook App that usually takes 1-3 days to process. If you are lucky, it will probably take about 6-8 hours.
Once it is approved, you can request the permission and get your application to go live.
Also use the Page Access token in your "access_token" field when invoking the API so that it will allow you to pull the "from" field, which contains the id and name of the user.

Authenticate in Microsoft Graph API programmatically (Python) with my own username and password?

I have an Excel (.xlsx) file in my Office 365 (Sharepoint Online). And I want to use Python to programmatically access (read and write the cells in the worksheets) the Excel file. It seems that Microsoft Graph is the right API to do it.
But I'm confused about how to authenticate with the Microsoft Graph API. It seems to need to register some kind of application in Azure AD and need the approval of my Azure AD admin. And when it gets to run, it uses the OAuth: pop up a window, let the user to login, then click the approve button.
The Python script I'm developing will be only used by myself. So I'd like to avoid OAuth because it is for multiple users. I don't need to support multiple users. And OAuth requires the user's consent in the UI. But what I want is a background authentication without any UI interaction.
So my question is how to authenticate with Microsoft Graph API programmatically in Python with my own Office 365 account (a Email username and a password)? Thank you very much!
Here is a link to the Microsoft Graph API Connect Sample for Python. I recommend following their example, getting it working, then try to change it for your own needs.
At the bottom of the page, they also give information on how to reach them here on StackOverflow.
Hope it helps,
J

Categories