How to update a Smartsheet with JSON data using Python? - python

I'm using the smartsheet-python-sdk and I have a simple JSON file that I want to update my Smartsheet sheet with.
I have succeeded in pulling sheet data down using the python requests library but I'm wondering how exactly I would post data.
Here is the code:
import requests
accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
baseUrl = "https://api.smartsheet.com/2.0/sheets"
req = requests.get(baseUrl, verify=False, headers = {"Authorization":"Bearer %s" %accessToken})
print(req.content)

I'd suggest checking out our API documentation, specifically the Python code samples section which provides a little more information. In addition, we provide code samples for almost all of our operations in the API documentation. Simply select Python when browsing the API docs.
In reference to posting data, that would depend on which operation you'd like to use. Here's an example of a "Create Sheet" operation:
# Create sheet in "Sheets" folder.
sheet = smartsheet.models.Sheet({
'name': 'newsheet',
'columns': [{
'title': 'Favorite',
'type': 'CHECKBOX',
'symbol': 'STAR'
}, {
'title': 'Primary Column',
'primary': True,
'type': 'TEXT_NUMBER'
}, {
'title': 'Status',
'type': 'PICKLIST',
'options': [
'Not Started',
'Started',
'Completed'
]
}
]
})
action = smartsheet.Home.create_sheet(sheet)
sheet = action.result
You can find similar examples for other operations. If you have additional questions, you can always reach out to our API support team: api#smartsheet.com

Related

How can I add attributes to my product using woocommerce api in python

I find difficulty on how to pass attributes to a product using the woocommerce rest api.
I successfully took the attribute id.
my function:
def assign_attribute_to_products(wcapi_yachtcharterapp,post_id,attribute_id):
#the post_id argument is the product id
data = {
"attributes": [
{
"id": attribute_id,
},
],
}
wcapi_yachtcharterapp.put("products/"+str(post_id), data).json()
The product is updated without passing the information of attribute_id.
Any idea how to fix this?
You are missing some data, take a look here
This works for me:
data = {'attributes': [{'id': 7,
'options': ['term01', 'term02'],
'variation': 'true',
'visible': 'true'}]}
even though the data type for visible and variation is a boolean in the docs, you have to set 'true' or 'false' with a string.
You have to include the options for variations.
Hope this helps.

How to create an account, and upload an image with that account with imgur

I am designing a python GUI, one of its functions is taking a screenshot, uploading it to Imgur and then getting the URL. Though I am having issues understanding the documentation (especially since it says you need to create an account through the API, but not how to do it.). Would anyone be able to explain how exactly to create an account and then upload an image using it?
Note: I am using PIL to get the screenshots, I would prefer you explain it as code written with the requests library or maybe curl (as that isn't too hard to move to python with requests), and I'll be saving only the refresh token in the program, as it would be hardcoded (But the user can change it) and I don't want the user to authenticate.
Thanks in advance.
Edit 1: Also, I will not use imgurpython as it is outdated.
First you have to create normal account on Imgur.
After loging to normal account you can go to https://api.imgur.com/oauth2/addclient to register application.
It needs application name and email. Type of authorization depends on how you will use it.
You should get API keys
Which you can use with API
To get information:
import requests
headers = {
'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}
#https://i.imgur.com/cvWgXFc.jpg
imageHash = 'cvWgXFc'
r = requests.get(f'https://api.imgur.com/3/image/{imageHash}', headers=headers)
print('status:', r.status_code)
data = r.json()
print(data)
print('size:', data['data']['size'])
Result:
status: 200
{'data': {'id': 'cvWgXFc', 'title': None, 'description': None, 'datetime': 1579572289, 'type': 'image/jpeg', 'animated': False, 'width': 506, 'height': 500, 'size': 89341, 'views': 8087, 'bandwidth': 722500667, 'vote': None, 'favorite': False, 'nsfw': False, 'section': None, 'account_url': None, 'account_id': None, 'is_ad': False, 'in_most_viral': False, 'has_sound': False, 'tags': [], 'ad_type': 0, 'ad_url': '', 'edited': '0', 'in_gallery': False, 'link': 'https://i.imgur.com/cvWgXFc.jpg', 'ad_config': {'safeFlags': ['onsfw_mod_safe', 'share', 'page_load'], 'highRiskFlags': [], 'unsafeFlags': ['not_in_gallery', 'sixth_mod_unsafe'], 'wallUnsafeFlags': [], 'showsAds': False}}, 'success': True, 'status': 200}
size: 89341
To upload:
import requests
import base64
headers = {
'Authorization': 'Client-ID f1XXXXXXXXXXXXX',
}
params = {
'image': base64.b64encode(open('images.png', 'rb').read())
}
r = requests.post(f'https://api.imgur.com/3/image', headers=headers, data=params)
print('status:', r.status_code)
data = r.json()
print(data)
BTW: you can see your registered applications and regenerate API keys (if you forget it) after login on https://imgur.com/account/settings/apps
You will need to use the Imgur API, which you can get from
here:
Once you get the API key from their site, you can start writing some code.

Facebook APIs to retrieve deleted campaign insights

I'm using the python Facebook API's SDK.
I am currently able to request and obtain campaign insights from the account level, with a snippet as such:
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adsinsights import AdsInsights as Insights
account = AdAccount(u"act_{}".format(account_id))
report_params = {
'time_increment': time_increment,
'time_range': {
'since': start_date.strftime("%Y-%m-%d"),
'until': end_date.strftime("%Y-%m-%d"),
},
'level': 'campaign'
}
insights = account.get_insights(fields=['campaign_name', 'spend'],
params=report_params, pending=True).execute()
The problem is that I can't seem to get information about a deleted campaign, that was active for a time, meaning that it has a spend value.
Here I read that adding a filtering might give me also DELETED or ARCHIVED campaigns, but in the get_insights documentation page I can't seem to find a field to filter on, and every try has been unsuccessful
I found the right filter, even though the effective_status is not obtainable through insight fields, you can use it in filters, in my case I included every possible campaign.effective_status. Using the following report_params
report_params = {
'time_increment': time_increment,
'time_range': {
'since': start_date.strftime("%Y-%m-%d"),
'until': end_date.strftime("%Y-%m-%d"),
},
'level': 'campaign',
'filtering': [{'field': 'campaign.effective_status',
'operator': 'IN',
'value': ['ACTIVE', 'PAUSED', 'DELETED', 'PENDING_REVIEW', 'DISAPPROVED',
'PREAPPROVED', 'PENDING_BILLING_INFO', 'CAMPAIGN_PAUSED', 'ARCHIVED',
'ADSET_PAUSED']}]
}

Trouble getting contact folders through microsoft graph api

I'm having difficulties getting Microsoft Graph to return two test Contact Folders that I have set up named Test and Test 2.
When I use v1.0:
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json'}
url = 'https://graph.microsoft.com/v1.0/me/contactFolders'
response = requests.get(url,headers=headers)
response_data = response.json()
print(response_data)
I get a blank value in the response:
{
'#odata.context': "https://graph.microsoft.com/v1.0/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': []
}
When I use the Beta, I get this which at least returns something but not what I'm looking for:
{
'#odata.context': "https://graph.microsoft.com/beta/$metadata#users('jacobdansey%40hotmail.com')/contactFolders",
'value': [{
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Contacts',
'wellKnownName': 'contacts'
}, {
'id': '*ID*',
'parentFolderId': '*ParentID*',
'displayName': 'Skype Contacts',
'wellKnownName': 'skypecontacts'
}]
}
I know I am connecting properly because when I ask for just contacts from https://graph.microsoft.com/v1.0/me/contacts, it returns the correct answer.
Any help would be greatly appreciated, thanks!
EDIT: Is there a difference between contact folders and contact lists?
i am assuming you want access to folder to get the contacts, if thats the case then you can directly get the contacts of that folder changing the get url
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_contacts

Magento 2 Rest API youtube video missing values

I have simple question regarding the Magento V1 media API. I'm trying to add a video to a product but it keeps telling me that is missing values.
I'm trying to add the data from Odoo (python) like this:
videoFile = {
"entry": {
'position': position,
'media_type': 'external-video',
'disabled': False,
'label': 'Holassss',
'types': ['image', 'small-image', 'thumbnail'],
'content': {
'base64_encoded_data': base64.b64encode(urllib.request.urlopen("https://img.youtube.com/vi/axwE9q7llEQ/0.jpg").read()).decode('ascii'),
'type': 'image/jpeg',
'name': '0.jpg'
},
'extension_attributes': {
'video_content': {
'media_type': 'external-video',
'video_provider': 'youtube',
'video_url': 'https://www.youtube.com/watch?v=axwE9q7llEQ',
'video_title': 'Titulo',
'video_description': 'Description',
'video_metadata': None,
}
}
}
}
cc = json.dumps(videoFile)
productUrl = url + "/index.php/rest/V1/products/" + productSku + "/media"
Eventually I'll add the content using the https://www.youtube.com/oembed?url=youtubeurl&format=json response.
I'm following the API documentation (http://devdocs.magento.com/swagger/index_20.html) for
catalogProductAttributeMediaGalleryManagementV1 (/V1/products/{sku}/media)
Error:
"message": "Option values that are not specified."
Please advice me on which values I'm missing and which I can leave NULL. Also let me know if there is a way to let Magento automatically get the description and other data (as on the admin panel) instead of providing it myself. last but not least, I think this documentation is missing some data. This already happened to me before with a different call and a couple of "optional" values were actually required. Is there another documentation web page?
Thank you very much.

Categories

Resources