Retrieve error code and message from Google Api Client in Python - python-2.7

I can't find a way to retrieve the HTTP error code and the error message from a call to a Google API using the Google API Client (in Python).
I know the call can raise a HttpError (if it's not a network problem) but that's all I know.
Hope you can help

This is how to access the error details on HttpError in Google API Client(Python):
import json
...
except HttpError as e:
error_reason = json.loads(e.content)['error']['errors'][0]['message']
...

Actually, found out that e.resp.status is where the HTTP error code is stored (e being the caught exception). Still don't know how to isolate the error message.

Class HttpError has a built-in _get_reason() method that calculate the reason for the error from the response content. _get_reason() source code.
The method has a kind of a misleading name for me. Because I've needed to get the value of the actual reason key from a response. Not a message key that _get_reason() is getting. But the method doing exactly what is asked by Valentin.
Example of a response, decoded from bytes to JSON:
{
"error": {
"errors": [{
"domain": "usageLimits",
"reason": "dailyLimitExceeded",
"message": "Daily Limit Exceeded. The quota will be reset at midnight Pacific Time(PT).
You may monitor your quota usage and adjust limits in the API Console:
https: //console.developers.google.com/apis/api/youtube.googleapis.com/quotas?project=908279247468"
}],
"code": 403,
"message": "Daily Limit Exceeded. The quota will be reset at midnight Pacific Time(PT).
You may monitor your quota usage and adjust limits in the API Console:
https: //console.developers.google.com/apis/api/youtube.googleapis.com/quotas?project=908279247468"
}
}

I found out HttpError has a status_code property. You can find it here.
from googleapiclient.errors import HttpError
...
except HttpError as e:
print(e.status_code)
...

You can use error_details or reason from HttpError in Google API Client(Python):
...
except HttpError as e:
error_reason = e.reason
error_details = e.error_details # list of dict
...

Related

Error on getting thumbnail_url field with Business Discovery API (Instagram Graph)

I'm trying to get thumbnail_url using Business Discovery API (LINK) and get an error.
Docs says: You can use both nested requests and field expansion to get public fields for a Business or Creator Account's media objects.
In IGMedia docs says: thumbnail_url - Public
So my working request:
{page-id}?fields=business_discovery.username(nike){media{media_url,media_type,children{media_url}}}.
Response:
And, if I add thumbnail_url field in request:
{page-id}?fields=business_discovery.username(nike){media{media_url,media_type,thumbnail_url,children{media_url}}}
Error:
{
"error": {
"message": "(#100) Please read documentation for supported fields.",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "A2sUKUDWUyAJjyQErkGjx7b"
}
}
It’s not the end. If I add thumbnail_url in children expansion, from response disappears all CAROUSEL_ALBUM posts.
Request:
{page-id}?fields=business_discovery.username(nike){media{media_url,media_type,children{media_url,thumbnail_url}}}
Response:
So my questions:
Is it a bug (seems like that it)? Or docs are not updated?
Is there other official way to get thumbnail_url using Business Discovery API?
Sorry about long read :)

Getting Unable to determine service/operation name to be authorized - AccessDeniedException Error on CreateParticipantConnection

I am trying to test the Amazon Connect Chat API in POSTMAN,
The first part StartChatContact tested/executed and i have got the ParticipantTokenback in response with ContactId & ParticipantId by using PUT: https://connect.us-east-1.amazonaws.com/contact/chat
But i am unable to run the POST: https://connect.us-east-1.amazonaws.com/participant/connection. here is the Request Syntax AWS has given:
POST /participant/connection HTTP/1.1
X-Amz-Bearer: ParticipantToken
Content-type: application/json
{
"ConnectParticipant": boolean,
"Type": [ "string" ]
}
i have tried the ParticipantToken as header parameter with name mentioned in documentation (X-Amz-Bearer: ParticipantToken) as well as used in Authorization as Type API Key & AWS Signature.
But can't get rid of this error. In response Headers i got x-amzn-ErrorType: AccessDeniedException and in body
{
"message": "Unable to determine service/operation name to be authorized"
}
How to remove this error? any solution please.

How to retrieve a list of contacts that belong to a specific group without limit error

I am unable to read contacts of a group.
I use the method contactGroups.List (https://people.googleapis.com/v1/contactGroups) to read all groups.
Then I read all the contacts with the supplied resource names for the given group with the people.get method (https://people.googleapis.com/v1/resourceName).
This works, but since a request is required for every contact, I immediately get the error:
Quota exceeded for quota metric 'Read requests' and limit 'Read requests per minute per user' of service 'people.googleapis.com' for consumer.
The limit is 75 requests / 60s / user.
Is there another way?
You could wrap your request with a try/catch and repeat the call after 2sec if a HttpError with code 429 (limit reached) is raised.
I'm not sure what language you are using so here is something generic.
Function my_call(request):
Try:
Return request.execute()
Catch HttpError as error:
If error.resp.status == 429:
sleep(2sec)
Return my_call(request)
Else:
Raise error
Where the request is built by the people.get method and the HttpError can be included from the googleapi module/library I believe.
Note that it would probably be more efficient (API calls wise) to use the people.connection.list method and retrieve
the contacts from the response instead.

Weird 500 error response from the androidpublisher API, nothing helpful to solve it,

I'm trying to verify a subscription token.
I'm receiving 500 (null) with no error message from Google AndroidPublisher API.
My request contains a bearer auth token and a simple GET for the v3 API:
https://www.googleapis.com/androidpublisher/v3/applications/{packagename}/purchases/subscriptions/sub1/tokens/{token}
And this is the response:
{
"error": {
"code": 500,
"message": null
}
}
Any idea what a 500 in the internal server could mean, or who I can contact to get this resolved?
Thanks!

how to correctly use integration response mapping in aws api gateway to return different http codes

I can't seem to set my integration response for errors using the amazon api gateway
I added an integration response but it does not return the 400 error, instead it continues to return 200 response with
{
"errorMessage": "foose",
"errorType": "Error",
"stackTrace": [
"exports.handler (/var/task/index.js:11:19)"
]
}
If you are using the Java, you need to throw an Exception. I made the mistake of trying to return the error information. The Lambda Error Regex parses the Exception message so if you throw this:
throw new Exception("Failed: Something bad happened!");
and replace your foo.* with Failed: .* it will use the 400 status code.
If you are using NodeJS, you can use context.fail('Failed: Something bad happened!'); to get the same result
.*"Failed:".*
This is the correct syntax for Lambda Regex.
Also, in nodeJS (as per your example above) it's easier to construct your own error object and add status for easier mapping, e.g.:
var myError = {}
myError.status = "userError"; //use this for 400 and "serverError" for 500
myError.message = err.stackTrace; //message body
Finally you need to return
context.fail(JSON.stringify(myError));
If you don't have response mapping set up properly.