Insert member - code: 400, message: Missing required field: member - python

I have valid json that is erroring out in the google groups member API. I left the oauth stuff out but here the executed code:
groupsSettings = build('groupssettings', 'v1', credentials=creds)
file=open('file.json')
data=json.load(file)
group = (data[i]["group"])
memberEmail = (data[i]["memberEmail"])
member = json.dumps({"email": memberEmail,"role": "MEMBER"})
adminRequest=admin.members().insert(groupKey=group,body=member)
groupsResponse=groupsRequest.execute()
The error is:
{'error': {'errors': [{'domain': 'global', 'reason': 'required', 'message': 'Missing required field: member'}], 'code': 400, 'message': 'Missing required field: member'}}
400 missing required field member. My member variable IS the member. I've tried the same groupKey and body in the google API explorer and it works without issue. I'm not sure what is wrong here but on the surface it appears that this should be valid. Any insight is greatly appreciated.

For anyone interested, this was a simple mistake of trying to push serialized json. It was corrected by using deserialized json in the following manner:
encode = json.dumps({"email": memberEmail,"role": "MEMBER"})
member = json.loads(encode)

Related

Smartsheet Send Email Failed by API

I'm trying to follow the API document and send an email by using my sheet.
Here is the code:
email = smartsheet.models.MultiRowEmail()
email.send_to = smartsheet.models.Recipient({
'email': 'testtest#gmail.com'
})
email.row_ids=[rowId]
email.column_ids = [columnID]
# Send rows via email
response = smartsheet_client.Sheets.send_rows(
sheet_id, # sheet_id
email)
It keeps giving me error message below,
{"response": {"statusCode": 400, "reason": "Bad Request", "content": {"errorCode": 1012, "message": "Required object attribute(s) are missing from your request: multipleRowsEmail.rowIds[], multipleRowsEmail.includeAttachments, multipleRowsEmail.includeDiscussions.", "refId": "lvre2gkxtm8m"}}}
Please advise, Thanks
Part of the problem is that you need to also specify the include_attachments property and the include_discussions property on the email object. These properties are required. (As a sidenote, I notice that the Python code snippet for Send Rows via Email in the API docs incorrectly omits these two properties...hopefully someone from Smartsheet sees this thread and can fix that issue in the docs.)
Not sure why the error message indicates that row IDs are missing from the request, as it looks like you're populating Row IDs. I'd suggest that you investigate to make sure that your [rowId] property (that you're using to set the value of email.row_ids in the request) actually is a populated array of Row IDs.
The following code example successfully sends an email that contains the specified 4 rows and 2 columns of data:
# specify IDs
sheet_id = 3932034054809476
row_ids = [4324392993613700, 5225480965908356, 657918269646724, 721881338537860]
column_ids = [6101753539127172, 4055216160040836]
# build email object
email = smartsheet.models.MultiRowEmail()
email.send_to = smartsheet.models.Recipient({
'email': 'testtest#gmail.com'
})
email.row_ids = row_ids
email.column_ids = column_ids
email.include_attachments = False
email.include_discussions = False
# send rows via email
response = smartsheet_client.Sheets.send_rows(
sheet_id,
email)

How to access error reason in Python google.cloud.storage upload methods?

I am using Google's google-cloud-storage Python package for GCS access. When I get a 403 error it could be for many different reasons. Google's SDK by default only provides this message:
('Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>)")
Using a debugger I can look deeper into the library and find _upload.py has a _process_response method where the true HTTP response can be found, with the following message as part of the result:
"message": "$ACCOUNT does not have storage.objects.delete access to $BLOB."
Q: Is there any way I can access this more useful error code or the raw response?
I am hoping to present to the user the difference between e.g. expired credentials and attempting to do something your credentials do not allow.
What version of google-cloud-storage are you using? With the latest, and this example:
from google.cloud import storage
client = storage.Client.from_service_account_json('service-account.json')
bucket = client.get_bucket('my-bucket-name')
blob = bucket.get_blob('test.txt')
try:
blob.delete()
except Exception as e:
print(e)
It prints the following:
403 DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.
The string representation here is roughly the same as e.message:
>>> e.message
'DELETE https://storage.googleapis.com/storage/v1/b/my-bucket-name/o/test.txt?generation=1579627133414449: $ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt.'
If you want more structure, you can use e._response.json():
>>> e._response.json()
{
'error': {
'code': 403,
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'errors': [{
'message': '$ACCOUNT does not have storage.objects.delete access to my-bucket-name/test.txt/test.txt.',
'domain': 'global',
'reason': 'forbidden'
}]
}
}

Cannot get response from mailchimp API

I need to connect mailchimp API through python script and GET data, that i'd later move to PowerBI for BI solutions.
I have read the documentation :
https://mailchimp.com/developer/guides/get-started-with-mailchimp-api-3/#Parameters
And I am trying to get a specific report
https://mailchimp.com/developer/reference/reports/#get_/reports/-campaign_id-
First I managed to connect to reports (https://mailchimp.com/developer/reference/reports/#get_/reports/) with the following code:
import requests
import json
r=requests.get("https://us11.api.mailchimp.com/3.0/reports/",
headers={"content-type": "application/json"},
auth=('anystring', 'myapikey')
,params={'fields':['id']})
data=r.json()
print(data)
r_dict = json.loads(r.text)
print(r.status_code)
print(r.text)
for i in r_dict:
print("key:", i,"val",r_dict[i])
Then I get some sort of output, which is not an error and it seems i connected since i retrieved data. Now I am trying to connect to a specific report, i set up an id, but it seems i cannot understand how to set up parameters properly. My code looks like this:
r=requests.get("https://us11.api.mailchimp.com/3.0/reports/{80419197aa}",
headers={"content-type": "application/json"},
auth=('anystring', 'myapikey')
,params={'fields':['id','campaign_title','type','list_id']})
data=r.json()
print(data)
r_dict = json.loads(r.text)
print(r.status_code)
print(r.text)
for i in r_dict:
print("key:", i,"val",r_dict[i])
Then i get this error:
{'type':
'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
'title': 'Resource Not Found', 'status': 404, 'detail': 'The requested
resource could not be found.', 'instance':
'13af7a5e-9868-4dd3-abd6-6c3b0b58983f'} 404
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Resource
Not Found","status":404,"detail":"The requested resource could not be
found.","instance":"13af7a5e-9868-4dd3-abd6-6c3b0b58983f"} key: type
val
http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
key: title val Resource Not Found key: status val 404 key: detail val
The requested resource could not be found. key: instance val
13af7a5e-9868-4dd3-abd6-6c3b0b58983f"
Could anyone help me how should i specify the parameters, because i suppose the mistake is there?
The reason why error 404 appears is because of the auth parameter, if you remove this parameter you will get the error 401 that is an invalid API Key, you would need to use the correct authentication method.
I would recommend that you first try it with a program like Postman, so that you discard some error outside the code.

AttributeError: 'str' object has no attribute 'sign'

When I run my python script, it gives me the following error. Not sure how to resolve it. I think so either the problem is in the version of python, I am using python 3.6 or It could be some packages not included. Please see the included image for error.
Thanks.
Error
Code:
f = open('c:\ExamCreator\My Project-9352ed298182.json')
key = f.read()
f.close()
credentials = ServiceAccountCredentials(
'CLIENT_EMAIL', key,
scope=
'https://www.googleapis.com/auth/admin.directory.user',
sub='amankahlon#test.grasslands.ab.ca'
)
http = httplib2.Http()
http = credentials.authorize(http)
userinfo = {
'primaryEmail': 'test1#test.grasslands.ab.ca',
'name': {
'givenName':'John','familyName':'Smith'
},
'password': 'Hello1'
}
service = build("admin", "directory_v1", http=http)
service.users().insert(body=userinfo).execute()
It appears that signer is being interpreted as a string, which inherently is just...a string. Not a dict or a list, both of which have attrs. Please post the function that is throwing this error. Also, this SO question and answer may be relevant to your interests.

Instagram API - Unable to generate proper access token (Python)

I am getting a JSON return like this:
{'data': [],
'meta': {'code': 200},
'pagination': {'deprecation_warning': 'next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead'}}
I am doing the following (where I'm omitting my personal info for the sake of this post):
CONFIG = {
'client_id': '',
'client_secret': '',
'redirect_uri': 'http://localhost:8515/oauth_callback'
}
api = InstagramAPI(**CONFIG)
Then I follow this link (inserting my info):
https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token
and I get an access token that allows me to do some things like the following:
api = InstagramAPI(access_token = access_token)
recent_media, next = api.user_recent_media()
but when I try to search for "nhl", for example:
url = requests.get('https://api.instagram.com/v1/tags/nhl/media/recent?access_token='+access_token)
url.json()
I get the error result seen at the top of this post.
On the other hand, using the apigee console to access the instagram API, I get a different access token (by allowing access to my instagram account) that gives the JSON response I'm looking for:
{"pagination":{"next_max_tag_id":"AQD9K8O5THfHA7yp1waOwivzT ...etc
including the data I want:
"data":[{"attribution":null,"tags":["nhl","zachayfucale","nhlpa"
...etc
My question is: how can I generate the access token that apigee was able to get?
I have also been able to acquire an access token a different way and I can include this method too if anyone asks, however it does not help solve my problem.

Categories

Resources