Text not being added when using Slides API to change text - python

I am currently trying to automatically update a slide in a Google Slide presentation with the Slides API and when I run the code it does not seem to update.
My current code is:
from datetime import datetime
from datetime import timedelta
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from apiclient import discovery
import httplib2
import json
import requests
import string
credentials = service_account.Credentials.from_service_account_file("credentials.json")
service = build('slides', 'v1', credentials=credentials)
#Getting the lunch for the next day
tomorrow = datetime.now() + timedelta(days=1)
url = tomorrow.strftime("https://udas.nutrislice.com/menu/api/digest/school/upper-dauphin-high/menu-type/lunch/date/%Y/%m/%d/")
data = requests.get(url)
test = data.json()['menu_items']
#Converting the lunch to look nice and printing it
lunchtext = json.dumps(test, indent=4, sort_keys=True, separators=('\n', ':'))
newlunchtext = lunchtext.replace(r'"', ' ')
newlunchtext = newlunchtext.replace(r'[', r'"')
newlunchtext = newlunchtext.replace(r']', r'"')
print(newlunchtext)
#Preparing to make the request
PRESENTATION_ID = '1DE0NxsRH6nWxlZGsOP-IzZD-qodw_0YLcMKE0QaQ7vE'
OBJECT_ID = 'g6d3de38d16_0_115'
requests = [
{
'insertText': {
'text': newlunchtext
},
'objectId': OBJECT_ID,
'insertionIndex': 0,
},
]
body = {
'requests': requests
}
update = service.presentations().batchUpdate(presentationId = PRESENTATION_ID, body = body).execute
print(update)
When I run it I get a response that looks like this:
"
French Toast Sticks & Sausage
Pulled Pork BBQ on Bun
McCain Crispy Tater Tots
Garden Salad
"
<bound method HttpRequest.execute of <googleapiclient.http.HttpRequest object at 0x105519c70>>
I am not sure why it is not working as I have the correct objectId and presentationId.

Answer:
There are a couple of issues with your code: the request JSON and the execution of the batchUpdate
More Information:
Firstly, as per the request documentation of the Slides API, both objectId and insertionIndex are representations that should be included inside the insertText resource. This needs to be changed as such:
requests = [
{
'insertText': {
'text': newlunchtext,
'objectId': OBJECT_ID,
'insertionIndex': 0,
},
},
]
The other issue is that yo uare not executing the batchUpdate only referencing it. You need to add parentheses in order to call the execute() function:
update = service.presentations().batchUpdate(presentationId = PRESENTATION_ID, body = body).execute()
As a side note - do remember that your service account also needs to have edit access to the Slide for it to be able to make changes.
References:
Google Slides API - Method: presentations.batchUpdate
Google Slides API - batchUpdate: Requests
Google Slides API - Python Quickstart - Set up the sample

Related

YT Data API throws Error 403 for just one video

I was using the YouTube Data API v3 to put the current view count in my video title just like in this video ( https://www.youtube.com/watch?v=BxV14h0kFs0 ) and it works!
I set it up in pythonanywhere for this video ID: 7vSw7XRivWY, worked like a charm, still running.
Then I tried to do the same thing for another video (ID: fDh-qcbIxtU ) and now it throws
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/youtube/v3/videos?part=snippet&alt=json returned "The request metadata is invalid.">
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
this error. I'm using the exact same code I used for the other ID, all I did was changing my variable for the video ID to the other one.
I've no clue why this happens. I retried it multiple times, I tried it with different API scopes, I tried it with another API client key - same result, not working.
In my python code I implemented a method change_title that takes the apiclient and the video ID as parameters then first fetches the view count & snippet part ( which contains the title ), edits the title section of the snippet, and then updates the video accordingly.
As already stated, it works for any other of my videos but not for this one (ID: fDh-qcbIxtU). I'm 100% sure I put in the correct ID and that I didn't change anything else.
Here's the whole file that is being executed:
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import time
scopes = ['https://www.googleapis.com/auth/youtube.force-ssl']
def like_video(youtube, videoID):
youtube.videos().rate(
id=videoID,
rating='like'
).execute()
def change_title(youtube, videoID):
video_list_response_statistics = youtube.videos().list(
id=videoID,
part="statistics"
).execute()
videos_list_statistics = video_list_response_statistics['items'][0]['statistics']
views = videos_list_statistics['viewCount']
print("Views: " + str(views))
snippetPart = youtube.videos().list(
id=videoID,
part="snippet"
).execute()
# Since the request specified a video ID, the response only contains one
# video resource. This code extracts the snippet from that resource.
videos_list_snippet = snippetPart['items'][0]['snippet']
print(videos_list_snippet)
videos_list_snippet['title'] += " [" + str(views) + " Views]"
videos_update_response = youtube.videos().update(
part='snippet',
body=dict(
id=videoID,
snippet=videos_list_snippet
)).execute()
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "credentials.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
i = 1
videoID = "fDh-qcbIxtU"
while True:
print("Update No" + str(i))
response = change_title(youtube, videoID)
print(response)
i += 1
time.sleep(1000)
if __name__ == "__main__":
main()
Thanks for anyone that's taking the time to try & solve my issue.
I was getting this error for my one particular youtube channel, and that was because it was managed by a brand account than its owner account.
It might not be helpful in your case but maybe it can help others facing this issue.
Just authorize with the right account.

403 Forbidden - cloud_storage_bucket get_media

import json
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials
from googleapiclient.discovery import build
json_file = 'my.json'
client_email = json.loads(open(json_file).read())['client_email']
private_key = json.loads(open(json_file).read())['private_key']
cloud_storage_bucket = 'my_bucket'
report_to_download = 'sales/salesreport_201907.zip'
credentials = SignedJwtAssertionCredentials(client_email, private_key,['https://www.googleapis.com/auth/devstorage.read_only','https://www.googleapis.com/auth/devstorage.read_write','https://www.googleapis.com/auth/devstorage.full_control'])
storage = build('storage', 'v1', http=credentials.authorize(Http()))
object_metadata = storage.objects().get(bucket = cloud_storage_bucket, object = report_to_download).execute()
object_content = storage.objects().get_media(bucket = cloud_storage_bucket, object = report_to_download).execute()
I can read the object_metadata, but when requesting object content I get:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/storage/v1/b/my_bucket/o/sales%2Fsalesreport_201907.zip?alt=media returned "Forbidden">
As u can see I added the three scopes in the credentials sections just to ensure to have all the permissions. Despite that I still getting the error.
I am using Python 2.7.
Edit:
I tried to download the file using gsutil and it works. So it isn't related with permissions.
Edit 2:
Changed the code to use a non deprecated library.
from logs import logger
import google.auth
import google.auth.transport.requests as tr_requests
from google.resumable_media.requests import Download
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my.json"
ro_scope=u'https://www.googleapis.com/auth/devstorage.full_control'
credentials,_ = google.auth.default(scopes=(ro_scope,))
transport = tr_requests.AuthorizedSession(credentials)
cloud_storage_bucket = 'pubsite_prod_rev_xxxx'
report_to_download = 'sales/salesreport_201907.zip'
#option 1
media_url='https://www.googleapis.com/download/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip?generation=1234&alt=media'
#option 2
media_url='https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip'
download = Download(media_url)
response = download.consume(transport)
print download.finished
When running the code with option 1 url the status is 200 but the response.content is just the metadata, has happened before with the get method.
When running the option 2, like get_media method, the error stills 403.
File "/anaconda2/envs/enviro27/lib/python2.7/site-packages/google/resumable_media/_helpers.py", line 93, in require_status_code
status_code, u'Expected one of', *status_codes)
google.resumable_media.common.InvalidResponse: (u'Request failed with status code', 403, u'Expected one of', 200, 206)
The doc of this code.
The solution has been to discover the true admin account.
Despite Google Play shows contact information to one email (the one that we believe to be the admin), the real admin is anothe account. After find it, we applied all the process explained here.
And with this code we are able to access the content:
from logs import logger
import google.auth
import google.auth.transport.requests as tr_requests
from google.resumable_media.requests import Download
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my.json"
ro_scope=u'https://www.googleapis.com/auth/devstorage.full_control'
credentials,_ = google.auth.default(scopes=(ro_scope,))
transport = tr_requests.AuthorizedSession(credentials)
cloud_storage_bucket = 'pubsite_prod_rev_xxxx'
report_to_download = 'sales/salesreport_201907.zip'
#option 1
media_url='https://www.googleapis.com/download/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip?generation=1234&alt=media'
#option 2
media_url='https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip'
download = Download(media_url)
response = download.consume(transport)
print download.finished

oauth2client is now deprecated

In the Python code for requesting data from Google Analytics ( https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py ) via an API, oauth2client is being used. The code was last time updated in July 2018 and until now the oauth2client is deprecated. My question is can I get the same code, where instead of oauth2client, google-auth or oauthlib is being used ?
I was googling to find a solution how to replace the parts of code where oauth2client is being used. Yet since I am not a developer I didn't succeed. This is how I tried to adapt the code in this link ( https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py ) to google-auth. Any idea how to fix this ?
import argparse
from apiclient.discovery import build
from google.oauth2 import service_account
from google.auth.transport.urllib3 import AuthorizedHttp
SCOPES = ['...']
DISCOVERY_URI = ('...')
CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.
VIEW_ID = '...'
def initialize_analyticsreporting():
"""Initializes the analyticsreporting service object.
Returns:l
analytics an authorized analyticsreporting service object.
"""
# Parse command-line arguments.
credentials = service_account.Credentials.from_service_account_file(CLIENT_SECRETS_PATH)
# Prepare credentials, and authorize HTTP object with them.
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# credentials will get written back to a file.
authed_http = AuthorizedHttp(credentials)
response = authed_http.request(
'GET', SCOPES)
# Build the service object.
analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
return analytics
def get_report(analytics):
# Use the Analytics Service Object to query the Analytics Reporting API V4.
return analytics.reports().batchGet(
body=
{
"reportRequests":[
{
"viewId":VIEW_ID,
"dateRanges":[
{
"startDate":"2019-01-01",
"endDate":"yesterday"
}],
"dimensions":[
{
"name":"ga:transactionId"
},
{
"name":"ga:sourceMedium"
},
{
"name":"ga:date"
}],
"metrics":[
{
"expression":"ga:transactionRevenue"
}]
}]
}
).execute()
def printResults(response):
for report in response.get("reports", []):
columnHeader = report.get("columnHeader", {})
dimensionHeaders = columnHeader.get("dimensions", [])
metricHeaders = columnHeader.get("metricHeader", {}).get("metricHeaderEntries", [])
rows = report.get("data", {}).get("rows", [])
for row in rows:
dimensions = row.get("dimensions", [])
dateRangeValues = row.get("metrics", [])
for header, dimension in zip(dimensionHeaders, dimensions):
print (header + ": " + dimension)
for i, values in enumerate(dateRangeValues):
for metric, value in zip(metricHeaders, values.get("values")):
print (metric.get("name") + ": " + value)
def main():
analytics = initialize_analyticsreporting()
response = get_report(analytics)
printResults(response)
if __name__ == '__main__':
main()
I need to obtain response in form of a json with given dimensions and metrics from Google Analytics.
For those running into this problem and wish to port to the newer auth libraries, do a diff b/w the 2 different versions of the short/simple Google Drive API sample at the code repo for the G Suite APIs intro codelab to see what needs to be updated (and what can stay as-is). The bottom-line is that the API client library code can remain the same while all you do is swap out the auth libraries underneath.
Note that sample is only for user acct auth... for svc acct auth, the update is similar, but I don't have an example of that yet (working on one though... will update this once it's published).

Google Calendar Integration with Django

Is there a fully fledged Django-based example of a Google Calendar integration? I was reading through Google's example page but their link at the bottom is outdated.
I'm specifically struggeling with the refresh token as Google's examples focus solely on how to get the access token. That's what I have so far:
#staff_member_required
def authorize_access(request):
return redirect(get_flow(request).step1_get_authorize_url())
#staff_member_required
def oauth2_callback(request):
credentials = get_flow(request).step2_exchange(request.GET['code'])
store = get_store()
store.put(credentials)
credentials.set_store(store)
return redirect('...')
def get_flow(request):
flow = client.flow_from_clientsecrets(os.path.join(CREDENTIAL_DIR, CLIENT_SECRET_FILE),
SCOPES,
redirect_uri='%s://%s/google-calendar/oauth2-callback/' % (request.META['wsgi.url_scheme'], request.META['HTTP_HOST'],))
flow.params['access_type'] = 'offline'
flow.params['approval_prompt'] = 'force'
return flow
def get_store():
return oauth2client.file.Storage(os.path.join(CREDENTIAL_DIR, ACCESS_TOKEN_FILE))
def has_valid_api_credentials():
credentials = get_store().get()
return credentials is not None
def build_service():
credentials = get_store().get()
if not credentials:
return None
elif credentials.access_token_expired:
http = credentials.refresh(httplib2.Http())
http = get_store().get().authorize(http)
else:
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
return service
def create_events(rental_request):
service = build_service()
event = service.events().insert(...).execute()
Researching a lot of different approaches I found out that server-to-server authentication is what I wanted. This way no user has to explicitly give permissions and acquired auth-tokens don't have to be renewed. Instead, using a service account, a server can make calls itself.
Before you can start coding, you have to setup such a service account and add it to your calendar that you want the service account to access. Google has written down the three steps to create an account here. Afterwards, go to https://calendar.google.com, locate on the left side of the screen the calendar you want to share with your new service account and click the triangle next to it. From the drop-down menu choose calendar settings. This takes you to a screen where you'll find the calendar-ID which you'll need later (so write it down) and also displays a tab at the top to share access to the calendar. As "person" insert the email address from the service account, give it the respective permissions and click save (if you don't click save the service account won't be added).
The code for this is actually pretty elegant:
import os
from datetime import timedelta
import datetime
import pytz
import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
service_account_email = 'XXX#YYY.iam.gserviceaccount.com'
CLIENT_SECRET_FILE = 'creds.p12'
SCOPES = 'https://www.googleapis.com/auth/calendar'
scopes = [SCOPES]
def build_service():
credentials = ServiceAccountCredentials.from_p12_keyfile(
service_account_email=service_account_email,
filename=CLIENT_SECRET_FILE,
scopes=SCOPES
)
http = credentials.authorize(httplib2.Http())
service = build('calendar', 'v3', http=http)
return service
def create_event():
service = build_service()
start_datetime = datetime.datetime.now(tz=pytz.utc)
event = service.events().insert(calendarId='<YOUR EMAIL HERE>#gmail.com', body={
'summary': 'Foo',
'description': 'Bar',
'start': {'dateTime': start_datetime.isoformat()},
'end': {'dateTime': (start_datetime + timedelta(minutes=15)).isoformat()},
}).execute()
print(event)
I'm using oauth2client version 2.2.0 (pip install oauth2client).
I hope this answer helps :)
Just a note here: Although the code works, as per https://github.com/googleapis/google-auth-library-python/blob/7a8641a7f0718c0dce413436f23691e8590face1/docs/index.rst, oauth2client has been deprecated recently in favour of google-auth library - https://github.com/googleapis/google-auth-library-python/tree/edfe24602051969e32917e82bcedd2bace43e260
You can find the documentation of the new library here - https://google-auth.readthedocs.io/en/latest/user-guide.html
To use the new library, the code can be written as
import datetime
from datetime import timedelta
import pytz
from google.oauth2 import service_account
from googleapiclient.discovery import build
service_account_email = "app-calendar#xxxxxxxxx.iam.gserviceaccount.com"
SCOPES = ["https://www.googleapis.com/auth/calendar"]
credentials = service_account.Credentials.from_service_account_file('google_calendar_credential.json')
scoped_credentials = credentials.with_scopes(SCOPES)
def build_service():
service = build("calendar", "v3", credentials=scoped_credentials)
return service
def create_event():
service = build_service()
start_datetime = datetime.datetime.now(tz=pytz.utc)
event = (
service.events()
.insert(
calendarId="primary",
body={
"summary": "Foo 2",
"description": "Bar",
"start": {"dateTime": start_datetime.isoformat()},
"end": {
"dateTime": (start_datetime + timedelta(minutes=15)).isoformat()
},
},
)
.execute()
)
print(event)
create_event()
As I do not have enough reputation to post this as comment, I am posting this as a separate post
As this post was quite a while ago I wanted to share my 2020 version of it. Thanks for this post. Helped me a lot to achieve my goal.
import datetime
from datetime import timedelta
import pytz
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
service_account_email = "INSERT_HERE"
SCOPES = ["https://www.googleapis.com/auth/calendar"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
filename="FILENAME.json", scopes=SCOPES
)
def build_service():
service = build("calendar", "v3", credentials=credentials)
return service
def create_event():
service = build_service()
start_datetime = datetime.datetime.now(tz=pytz.utc)
event = (
service.events()
.insert(
calendarId="CALENDARID#group.calendar.google.com",
body={
"summary": "Foo",
"description": "Bar",
"start": {"dateTime": start_datetime.isoformat()},
"end": {
"dateTime": (start_datetime + timedelta(minutes=15)).isoformat()
},
},
)
.execute()
)
print(event)
create_event()
2022
credits to #joey Coder(i would have added this as comment but its too long)
If you want your website or app to make events or calendars without have to use the Google accounts of the users you should use service accounts.
In https://console.cloud.google.com/ choose your project or start new one.
In the navigation menu choose "APIs & Services"
enable new APIs and then look up "calendar API", enable the API
Under "APIs & Services">"Credentials", select "Create Credentials" and click on "service account", fill in the desired name, and continue. Set role as owner(or other desired)(owner gives full access you you might want to switch to something less powerful). Click "Done"
This will redirect you to the credentials page.
Under the "Service accounts" click on the desired account(this will redirect you to the IAM & Admin panel)
Under the tab "Keys" click "ADD KEY" and select json, this will download a json file to your computer.
in the Calendar page in google
get and add the calendar ID to the admin panel under AgendaClients "CalendarId"
add the service account to the people shared as admin (make changes to events)
This is how it looks like in my django project:
signals.py
import datetime
import json
import os
from django.db.models.signals import post_delete, post_save
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.service_account import ServiceAccountCredentials
from .models import Event
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/calendar"]
def get_service(refresh=False):
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
json.loads(os.environ.get("client_secret")), scopes=SCOPES
)
# or if you have a file
# credentials = ServiceAccountCredentials.from_json_keyfile_name(
# filename="file.json", scopes=SCOPES
# )
service = build("calendar", "v3", credentials=credentials)
return service
def handle_event(sender, created, instance, **kwargs):
"""this function creates the events in the google agenda and updates them if changed in the website"""
service = get_service()
event = instance
if not event.end_date:
event.end_date = event.start_date
if not event.end_time and event.start_time:
event.end_time = event.start_time
elif not event.end_time:
event.end_time = datetime.datetime.min.time()
if not event.start_time:
event.start_time = datetime.datetime.min.time()
if event.end_date < event.start_date:
event.end_date, event.start_date = event.start_date, event.end_date
queryset = Event.objects.filter(
id=event.id
) # https://stackoverflow.com/questions/1555060/how-to-save-a-model-without-sending-a-signal
# this is used so that we can update the google event within this signal without reshooting this signal(signals shot every time an object is saved)
event = {
"summary": event.description,
"location": event.location or "",
"description": (event.description + " " + event.summary),
"start": {
"dateTime": datetime.datetime.combine(
event.start_date, event.start_time
).isoformat(),
"timeZone": "Europe/Amsterdam",
},
"end": {
"dateTime": datetime.datetime.combine(
event.end_date, event.end_time
).isoformat(),
"timeZone": "Europe/Amsterdam",
},
"recurrence": [],
"reminders": {},
}
if created or not instance.google_link:
try:
event = (
service.events()
.insert(
calendarId=os.environ.get("calendarId"),
body=event,
)
.execute()
)
queryset.update(google_link=event["id"])
except HttpError as error:
# print("An error occurred: %s" % error)
pass
else:
try:
event = (
service.events()
.update(
calendarId=os.environ.get("calendarId"),
body=event,
eventId=instance.google_link,
)
.execute()
)
queryset.update(google_link=event["id"])
except HttpError as error:
# print("An error occurred: %s" % error)
pass
# print("#############ADDED NEW #############")
def delete_event(sender, instance, **kwargs):
"""this function deletes an event from google agenda when deleted in the website"""
try:
service = get_service()
service.events().delete(
calendarId=os.environ.get("CalendarId"),
eventId=instance.google_link,
).execute()
except:
pass
post_save.connect(handle_event, sender=Event)
post_delete.connect(delete_event, sender=Event)
models.py
class Event(models.Model):
summary = models.CharField(max_length=50)
description = models.CharField(max_length=50, null=True, blank=True)
start_date = models.DateField()
start_time = models.TimeField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)
location = models.CharField(max_length=50, null=True, blank=True)
google_link = models.CharField(max_length=150, null=True, blank=True)
# google link is used to edit events in google if you change them in your website
Feel free to ask any questions or point out anything

How to update stock_quantity in python using woocommerce api

Trying to follow the api sample from http://woothemes.github.io/woocommerce-rest-api-docs/?python#update-a-product-attribute, but it's not updating the stock_quantity. I've confirmed that I have read/write access set for my api key.
I get the following error from the code below:
u'errors': [{u'message': u'No product data specified to edit product', u'code': u'woocommerce_api_missing_product_data'}]}
Any ideas?
import sys
import json
from woocommerce import API
wcapi = API(
url="http://(myurl)",
consumer_key="ck_<mykey>",
consumer_secret="cs_<mysecret>",
timeout=30
)
data = {
"stock_quantity": 5
}
print(wcapi.put("products/18", data).json())
Ok figured it out. I was using the wrong version of the api documentation. I should have been using v3. The correct code is:
import sys
import json
from woocommerce import API
wcapi = API(
url="http://(myurl)",
consumer_key="ck_<mykey>",
consumer_secret="cs_<mysecret>",
timeout=30
)
data = {
"product": {
"stock_quantity": 5
}
}
print(wcapi.put("products/18", data).json())
In the newest version of the API I found wrapping the product info in the "product" dictionary is not needed:
import sys
import json
from woocommerce import API
wcapi = API(
url="http://(myurl)",
consumer_key="ck_<mykey>",
consumer_secret="cs_<mysecret>",
timeout=30
)
data = {
"stock_quantity": 5
}
print(wcapi.put("products/18", data).json())

Categories

Resources