I am pretty much using the sample from google's own site
https://developers.google.com/apps-script/api/how-tos/execute
The relevant part of the sample python script is replicated below
from __future__ import print_function
from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
def main():
"""Runs the sample.
"""
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
# Setup the Apps Script API
SCOPES = 'https://www.googleapis.com/auth/script.projects'
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('script', 'v1', http=creds.authorize(Http()))
if __name__ == '__main__':
main()
I am getting the following error
File "test.py", line 67, in <module>
main()
File "test.py", line 22, in main
service = build('script', 'v1', http=creds.authorize(Http()))
File "C:\Users\pedxs\Anaconda2\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\pedxs\Anaconda2\lib\site-packages\googleapiclient\discovery.py", line 232, in build
raise e
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/discovery/v1/apis/script/v1/rest returned "Request contains an invalid argument.">
I had this exact code working just a week ago. line 22 uses the discovery build function, which as I understand is sending credentials to Google's API authenticator server "https://www.googleapis.com/discovery/v1/apis/script/v1/rest". I am suspecting this is a problem on Google's side because even their sample code does not work.
I have tried creating a new Google Cloud Platform and getting a new credentials.json file. I also tried authenticating with a different email account.
i encountered same error.
i have used that code more than 1 year ago.
but suddenly i could not use since Feb-23.
so i adopted another method, and i used post request with oauth2.
i think you need your google service account renew.
i think something change with scope ui...
good luck!
■python code
from oauth2client import client
def request_to_gas():
credentials = client.OAuth2Credentials(
access_token=None,
client_id={your_client_id},
client_secret={your_client_secret},
refresh_token={your_refresh_token},
token_expiry=None,
token_uri=GOOGLE_TOKEN_URI,
user_agent=None,
revoke_uri=GOOGLE_REVOKE_URI)
credentials.refresh(httplib2.Http()) # refresh the access token
my_url = "your_google_apps_script_web_url"
myheaders = {'Authorization': 'Bearer {}'.format(credentials.access_token),
"Content-Type": "application/json"
}
response = requests.post(my_url,
data=json.dumps({
'localdate' : '2019/02/23 12:12:12'}),
headers=myheaders
)
add thins code and publish as web app.
■google apps script code
function doPost(e) {
var params = JSON.parse(e.postData.getDataAsString()); // ※
var value = params.localdate; // get python code -- 2019/02/23 12:12:12
// here is your google apps script api
do_something();
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify({ message: "success!" }));
return output;
}
In your situation, there are 2 patterns.
Pattern 1:
Use authorization script at Quickstart.
Sample script:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
def main():
# Setup the Apps Script API
SCOPES = ['https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/drive']
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret.json', SCOPES)
creds = flow.run_local_server()
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('script', 'v1', credentials=creds)
scriptId = "### script ID ###" # Please set this
request = {"function": "myFunction", "parameters": ["sample"], "devMode": True}
response = service.scripts().run(body=request, scriptId=scriptId).execute()
print(response)
if __name__ == '__main__':
main()
Pattern 2:
If you want to use the script in your question, please modify your script as follows. This is discussed at here and here.
Sample script:
from __future__ import print_function
from googleapiclient.discovery import build
from oauth2client import file as oauth_file, client, tools
def main():
SCOPES = ['https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/drive']
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('script', 'v1', credentials=creds)
scriptId = "### script ID ###" # Please set this
request = {"function": "myFunction", "parameters": ["sample"], "devMode": True}
response = service.scripts().run(body=request, scriptId=scriptId).execute()
print(response)
if __name__ == '__main__':
main()
Sample script of GAS side:
function myFunction(e) {
return "ok: " + e;
}
Result:
You can retrieve the following response from above both scripts.
{
"response": {
"#type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse",
"result": "ok: sample"
},
"done": True
}
Note:
Before you use above scripts, please setup for using it. You can see the official document at below.
Executing Functions using the Apps Script API
Method: scripts.run
In my environment, I could confirm that above both patterns can be used. But if in your environment, those were not used, I apologize.
Related
I'm coding a Python app to automatize invoices via a google docs API but I'm having errors with the code and I don't know how to solve it
# [START docs_quickstart]
from __future__ import print_function
import os.path
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly','https://www.googleapis.com/auth/documents','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.readonly']
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('docs', 'v1', credentials=creds)
title = 'My Document'
body = {
'title': title
}
document = service.documents().create(body=body).execute()
print('Created document with title: {0}'.format(
document.get('title')))
except HttpError as err:
print(err)
if __name__ == '__main__':
main()
# [END docs_quickstart]
and this is the error code that I'm having
PS D:\Universidad\Proyectos de Programacion Propia\python\googledocsinvoice.restapi> python quickstart.py
<HttpError 403 when requesting https://docs.googleapis.com/v1/documents?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'#type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'service': 'docs.googleapis.com', 'method': 'google.apps.docs.v1.DocumentsService.CreateDocument'}}]">
if you know some guide or complete tutorial to Automate Document Creation with the Google Docs API like invoices and replace encapsulation values like {{productID}} I will be very grateful
I saw the Automate document creation of google channel but it wasn't very helpful https://youtu.be/-dX-fWb3ogE
Best Regards
Make sure you also enable the Docs API in your Developers Console.
Delete the credential file ~/.credentials.json and token.json (if you ran the code before
Change the scope variable used for reading docs
var SCOPES = ['https://www.googleapis.com/auth/documents.readonly'];
to
var SCOPES = ['https://www.googleapis.com/auth/documents'];
Check more info on https://developers.google.com/docs/api/reference/rest/v1/documents/create#authorization-scopes and https://developers.google.com/docs/api/how-tos/authorizing
After the execution of code, API will authenticate again and then the issue should be resolved.
I want to get list of users google api but face an issue
My steps are:
Created Service account with domain wide delegation and listed scopes (same as in the script)
Downloaded json file with private key
On execution of the next script
import google.auth
import google.auth.transport.requests
from google.oauth2 import service_account
import requests
credentials = service_account.Credentials.from_service_account_file('key.json', scopes=[
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.googleapis.com/auth/admin.directory.group.member',
'https://www.googleapis.com/auth/admin.directory.user.security',
'https://www.googleapis.com/auth/admin.directory.user.readonly'
])
auth_req = google.auth.transport.requests.Request()
refresh = credentials.refresh(auth_req)
response = requests.get('https://www.googleapis.com/admin/directory/v1/users?domain=domain.com',
headers={'Authorization': f'Bearer {credentials.token}'})
Response is:
{
"error": {
"code": 403,
"message": "Not Authorized to access this resource/api",
"errors": [
{
"message": "Not Authorized to access this resource/api",
"domain": "global",
"reason": "forbidden"
}
]
}
}
I ended up with the next solution
console.cloud.google.com => APIs & Services > Credentials
Create Credentials > OAuth Client ID > Desktop APp
Saved key as 'credentials.json'
The rest id described here
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']
#
def main():
"""Shows basic usage of the Admin SDK Directory API.
Prints the emails and names of the first 10 users in the domain.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print('Refreshing token')
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'python_admin.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('admin', 'directory_v1', credentials=creds)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
service_users = service.users()
results = service_users.list(customer='my_customer', maxResults=10, orderBy='email').execute()
users = results.get('users', [])
service_users.insert()
if not users:
print('No users in the domain.')
else:
print('Users:')
for user in users:
print(u'{0} ({1})'.format(user['primaryEmail'],
user['name']['fullName']))
if __name__ == '__main__':
main()
For the first time I had to authorize request in the browser, after that access and refresh token could be used for next requests. Except manual authorization for the first time - works fine
I've been finding such a mix of code examples. But nothing with a maintained library (google-auth) + full working example. google-api-python-client and oauth2client are no longer supported (https://github.com/googleapis/google-api-python-client/issues/651).
Here's a working example with deprecated libraries, but I'd like to see some examples that allow full access to the api (searching by albumId currently doesn't work with this library):
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Photo v1 API
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))
# Call the Photo v1 API
results = service.albums().list(
pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
print('No albums found.')
else:
print('Albums:')
for item in items:
print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))
You want to use google_auth instead of oauth2client, because oauth2client is deprecated.
You have already been able to use Photo API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
For example, the sample script for authorizing can be seen at the Quickstart of Drive API with python. You can see the method for installing the library. Using this, your script can be modified as follows.
Modified script:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
def main():
credentialsFile = 'credentials.json' # Please set the filename of credentials.json
pickleFile = 'token.pickle' # Please set the filename of pickle file.
SCOPES = ['https://www.googleapis.com/auth/photoslibrary']
creds = None
if os.path.exists(pickleFile):
with open(pickleFile, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentialsFile, SCOPES)
creds = flow.run_local_server()
with open(pickleFile, 'wb') as token:
pickle.dump(creds, token)
service = build('photoslibrary', 'v1', credentials=creds)
# Call the Photo v1 API
results = service.albums().list(
pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
print('No albums found.')
else:
print('Albums:')
for item in items:
print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))
if __name__ == '__main__':
main()
About the script for retrieving the album list, your script was used.
When you run this script, at first, the authorization process is run. So please authorize the scope. This process is required to run only one time. But if you want to change the scopes, please delete the pickle file and authorize again.
References:
the Quickstart of Drive API with python
google-auth-library-python
google_auth_oauthlib package
Method: albums.list
If I misunderstood your question and this was not the direction you want, I apologize.
Added 1:
If you want to use the method of mediaItems.search, how about the following sample script? About the script for authorizing, please use above script.
Sample script:
service = build('photoslibrary', 'v1', credentials=creds)
albumId = '###' # Please set the album ID.
results = service.mediaItems().search(body={'albumId': albumId}).execute()
print(results)
Added 2:
You want to remove googleapiclient from my proposed above sample script.
You want to retrieve the access token using google_auth_oauthlib.flow and google.auth.transport.requests.
You want to retrieve the media item list in the specific album using request of python without googleapiclient.
I think that in this case, "Method: mediaItems.search" is suitable.
If my understanding is correct, how about this sample script?
Sample script:
Before you use this script, please set the variable of albumId.
from __future__ import print_function
import json
import pickle
import os.path
import requests
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
def main():
credentialsFile = 'credentials.json'
pickleFile = 'token.pickle'
SCOPES = ['https://www.googleapis.com/auth/photoslibrary.readonly']
creds = None
if os.path.exists(pickleFile):
with open(pickleFile, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentialsFile, SCOPES)
creds = flow.run_local_server()
with open(pickleFile, 'wb') as token:
pickle.dump(creds, token)
albumId = '###' # <--- Please set the album ID.
url = 'https://photoslibrary.googleapis.com/v1/mediaItems:search'
payload = {'albumId': albumId}
headers = {
'content-type': 'application/json',
'Authorization': 'Bearer ' + creds.token
}
res = requests.post(url, data=json.dumps(payload), headers=headers)
print(res.text)
if __name__ == '__main__':
main()
Note:
In this case, you can use the scope of both https://www.googleapis.com/auth/photoslibrary.readonly and https://www.googleapis.com/auth/photoslibrary.
Reference:
Method: mediaItems.search
I get this error whenever I try running that code : HttpError 404 when requesting https://sheets.googleapis.com/v4/spreadsheets/1KpJw640oNNwVfEasGoSffrfoyC7i1ryHakdWZmr-AX4/values:batchUpdate?alt=json returned "Requested entity was not found.">
I am trying to update a Google Sheet file with the help of this API but the only scope that I manage using is "readonly". I tried deleting the token.pickle file : I get identified so the request isn't requiring authentification credentials. But I don't know why I would still get an error. So basically all I can do is extracting data from a sheet but not overwrite cells.
from __future__ import print_function
from pprint import pprint
from googleapiclient import discovery
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https:// www.googleapis.com/auth/spreadsheets'] #what I have changed from the Python Quickstart code from the Google Sheets API website
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = discovery.build('sheets', 'v4', credentials=creds)
spreadsheet_id = '1KpJw640oNNwVfEasGoSffrfoyC7i1ryHakdWZmr-AX4' #this id is correct and still I get an error
batch_update_values_request_body = {
'value_input_option': 'USER_ENTERED',
'data': [
{
"range": "Control Panel!A:B",
"majorDimension": "ROWS",
"values": [[1,2]]
}
],
}
request = service.spreadsheets().values().batchUpdate(spreadsheetId=spreadsheet_id,
body=batch_update_values_request_body)
response = request.execute()
pprint(response)
I can't write data to google sheets via python. I did everything like in Google Sheets Api example but it still doesn't work. This is my project:
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']
SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'
def main():
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
values = {'values': [['one','two','three']]}
result = service.spreadsheets().values().append(
spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
valueInputOption='RAW',
body=values).execute()
print('{0} cells updated.'.format(result.get('updatedCells')));
if __name__ == '__main__':
main()
And it gives me this errors:
C:\Users\Victor\Anaconda3\python.exe "C:/Users/Victor/Desktop/NEW try/try1.py"
Traceback (most recent call last):
File "C:/Users/Victor/Desktop/NEW try/try1.py", line 31, in <module>
main()
File "C:/Users/Victor/Desktop/NEW try/try1.py", line 27, in main
body=values).execute()
File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\Victor\Anaconda3\lib\site-packages\googleapiclient\http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1RaeZ4QpT3-ZCcIfBDKdtQJt0WDJKiLnBsB3dTC2PoGg/values/Sheet2%21A1%3AA10:append?valueInputOption=RAW&alt=json returned "Request had insufficient authentication scopes.">
Process finished with exit code 1
I pin a screenshot that prooves that i have all the permisions in the api.
Google API
Thank you in advance.
From my understanding, exit code 1 "means there was some issue / problem which caused the program to exit." Trying to debug your code, it seems like your indentation is off. Everything between lines 11 and 25 should have at least one indent so it's considered to be apart of the main() function. Like so:
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file']
SAMPLE_SPREADSHEET_ID = 'ID'
SAMPLE_RANGE = 'Sheet2!A1:A10'
def main():
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
values = {'values': [['one','two','three']]}
result = service.spreadsheets().values().append(
spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE,
valueInputOption='RAW',
body=values).execute()
print('{0} cells updated.'.format(result.get('updatedCells')));
if __name__ == '__main__':
main()
Also, the Google Sheets API Quick Start for Python gets updated frequently, so I would try using the new code featured here. Let us know what happens after you fix the indentation or you decide to go with Google's updated code.