POST request on GAE (status_code = 500) - python

I am running the following code on Google App Engine Standard Python 2.7 version
import requests
import re
import datetime
ReviewsURL='https://play.google.com/store/getreviews'
payload = { 'reviewType': '0'
, 'pageNum': 0 #loads max 40 reviews for each page number
, 'id': 'net.one97.paytm'
, 'reviewSortOrder': '0'
, 'xhr':'1'
}
r = requests.post(
url=ReviewsURL,
data=payload,
headers={
'X-Requested-With': 'XMLHttpRequest'
}
)
print r.text.encode('cp850', errors='replace').decode("unicode-escape")
This code used to run fine and output the latest 40 reviews but now it gives the following output:
Status_code = 500
When I run the same code on my Mac, it works fine as well.

Taking into account the message “Server Error: None for url” looks like the URL cannot get reached. 500 are mainly caused by 2 reasons:
Dynamic responses, which are limited to 32MB. If a script handler generates a response larger than this limit, the server sends back an empty response with a 500 Internal Server Error status code.
Exceeded response time. A request handler has a limited amount of time to generate and return a response to a request, typically around 60 seconds. There are several root causes listed here.
There are many reasons that can cause a response time exceeding. I expect that you find yours in the documentation. I am not able to guess one of them based on the information we have.

Related

how to get authorize code for adultwork using python

Given the rest api instruction on the site:
https://developers.adultwork.com/Site/UnifiedLogin/AuthorizationCode
Having tried a number of variants from the many available resources all of which throw 400 type errors (usually 404 or 405 error), would like to have a piece of code such as:
def get_access_token():
url = 'https://api.adultwork.com' + '/OAuth/Authorize'
data = {"grant_type": "client_credentials"}
auth=(cr.api_key, cr.api_secret)
r = requests.get(url, data=data, auth=auth)
print(r.status_code)
return r
There appear to only be a few components, so presumably the above is a close guess.
The credential strings cr.api_key and cr.api_secret are given to the user which presumably are sufficient for the above.
So the question is how to implement this (in python) to get the access code ?
An explanation would be most helpful too.

Instagram Api Requests Systematically Refused

I have been using client id and auth token of instagram api for a while to make requests with urllib and json. Since a few days, any client id/auth token I create for an instagram account returns systematically "HTTP Error 400: BAD REQUEST" when I make a request, it can be for like, follow, or unfollow, it always returns this error. The script is Python 2.7 based.
It was working great before, and the keys created before this happened still work great! I tried to create new accounts and new keys from usa with proxies, but the error persist..
Here is the part of the code :
user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7’
headers = { 'User-Agent' : user_agent,
"Content-type": "application/x-www-form-urlencoded”
def likePicture(pictureId):
liked = 0
try:
urlLike = "https://api.instagram.com/v1/media/%s/likes"
values = {'access_token' : auth_token,
'client_id' : client_id}
newLike = urlLike % (pictureId)
data = urllib.urlencode(values)
req = urllib2.Request(newLike,data,headers)
response = urllib2.urlopen(req)
result = response.read()
dataObj = json.loads(result)
liked = 1
except Exception, e:
print e
return liked
The print e gives me systematically "HTTP Error 400: BAD REQUEST", even if the key is brand new and the account brand new. And this code is working like a charm on older keys (from a week ago).
Any idea or suggestion? Maybe blocked somehow by instagram because I created to many client id/auth token? If it is the case, how to do to resolve this situation? (I already tried to use different proxies, unsuccessful, so how would they track that?). If someone finds a solution to this problem I will be infinitely grateful to him!
Cheers, Kevin
First of all:
You may also receive responses with an HTTP response code of 400 (Bad
Request) if we detect spammy behavior by a person using your app.
These errors are unrelated to rate limiting.
Have you read the "Limits" from API docs?
When calling Instagram API methods it send two HTTP headers:
X-Ratelimit-Remaining: the remaining number of calls available to your app within the 1-hour window
X-Ratelimit-Limit: the total number of calls allowed within the 1-hour window
So check if you've reached the limit.
Keep in mind that multiples calls in a short time window is considered abusive.
Read more:
Limits
P.S: It's not necessary forge headers in order to make API calls! It isn't web scraping!

Python OAuth Error "Invalid Grant"

I work at a small company whose mail is hosted through Google. As one of the administrators responsible for managing employee accounts I'm attempting to automate some tasks using Google APIs and one or more dedicated service accounts. I wrote the short Python script below to request an access token according to the documentation Google provides but I continue to get an "invalid_grant" error.
I've done the following:
Logged into the Google Developers Console as the service account and created a project
Created a client ID of type service account
Logged into the Google Admin Console and added client email address and scope URLs I wish the account to have access to
Searches online have yielded answers indicating that either the system time of the machine is off, the limit for refresh tokens has been exceeded or that the client ID instead of the client email has been used. I've synchronized the time on a Windows machine and a Linux machine prior to running the script and the same error appears. I'm not even getting an access token so I doubt the refresh limit has been exceeded. As can be seen below, the iss value is set to the client email address ending with "#developer.gserviceaccount.com".
I suspect that there is something else I am missing and, as I'm new to Python, I suspect it's something embarrassingly simple. Perhaps someone can assist?
import json
import time
import base64
import requests
from datetime import datetime
utc = datetime.utcnow()
iat = time.mktime(utc.timetuple())
exp = iat + 3600
jwt_header = {
"alg":"RS256",
"typ":"JWT"
}
jwt_claim_set = {
"iss":"pleasehelpme#developer.gserviceaccount.com",
"scope":"https://www.googleapis.com/auth/admin.directory.user",
"aud":"https://www.googleapis.com/oauth2/v3/token",
"iat":int(iat),
"exp":int(exp),
"sub":"someadminfrustrated#googleoauth.com"
}
jwt_jws = bytearray(str(jwt_header) + '.' + str(jwt_claim_set))
jwt_unencoded = str(jwt_header) + '.' + str(jwt_claim_set) + '.' + str(jwt_jws)
jwt_encoded = base64.urlsafe_b64encode(str(jwt_unencoded))
url = 'https://www.googleapis.com/oauth2/v3/token'
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
payload = {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': jwt_encoded
}
response = requests.post(url, headers=headers, params=payload)
print response
print response.json()
The output is the following:
<Response [400]>
{u'error_description': u'Bad Request', u'error': u'invalid_grant'}
I would look at the output http and url to make sure, but it does seem like you are having authentication issues.
I ran into a similar, albeit less complicated problem, and couldn't get my Google Developer credentials in through a python script. I ended up using my Chrome browser cookies with this script to get through that part: http://n8henrie.com/2014/05/decrypt-chrome-cookies-with-python/
Makes everything pretty simple:
url = 'http://www.example.com'
s = requests.Session()
cookies = pyCookieCheat.chrome_cookies(url)
s.get(url, cookies = cookies)

Python URLLib / URLLib2 POST

I'm trying to create a super-simplistic Virtual In / Out Board using wx/Python. I've got the following code in place for one of my requests to the server where I'll be storing the data:
data = urllib.urlencode({'q': 'Status'})
u = urllib2.urlopen('http://myserver/inout-tracker', data)
for line in u.readlines():
print line
Nothing special going on there. The problem I'm having is that, based on how I read the docs, this should perform a Post Request because I've provided the data parameter and that's not happening. I have this code in the index for that url:
if (!isset($_POST['q'])) { die ('No action specified'); }
echo $_POST['q'];
And every time I run my Python App I get the 'No action specified' text printed to my console. I'm going to try to implement it using the Request Objects as I've seen a few demos that include those, but I'm wondering if anyone can help me explain why I don't get a Post Request with this code. Thanks!
-- EDITED --
This code does work and Posts to my web page properly:
data = urllib.urlencode({'q': 'Status'})
h = httplib.HTTPConnection('myserver:8080')
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
h.request('POST', '/inout-tracker/index.php', data, headers)
r = h.getresponse()
print r.read()
I am still unsure why the urllib2 library doesn't Post when I provide the data parameter - to me the docs indicate that it should.
u = urllib2.urlopen('http://myserver/inout-tracker', data)
h.request('POST', '/inout-tracker/index.php', data, headers)
Using the path /inout-tracker without a trailing / doesn't fetch index.php. Instead the server will issue a 302 redirect to the version with the trailing /.
Doing a 302 will typically cause clients to convert a POST to a GET request.

Google Data API authentication

I am trying to get my Django app (NOT using Google app engine) retrieve data from Google Contacts using Google Contacts Data API. Going through authentication documentation as well as Data API Python client docs
First step (AuthSubRequest) which is getting the single-use token works fine. The next step(AuthSubSessionToken), which is upgrade single-use token to a session token. The python API call UpgradeToSessionToken() simply didn't work for me it gave me NonAuthSubToken exception:
gd_client = gdata.contacts.service.ContactsService()
gd_client.auth_token = authsub_token
gd_client.UpgradeToSessionToken()
As an alternative I want to get it working by "manually" constructing the HTTP request:
url = 'https://www.google.com/accounts/AuthSubSessionToken'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'AuthSub token=' + authsub_token,
'User-Agent': 'Python/2.6.1',
'Host': 'https://www.google.com',
'Accept': 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
'Connection': 'keep-alive',
}
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req)
this gives me a different error:
HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: Moved Temporarily
What am I doing wrong here? I'd appreciate help/advice/suggestions with either of the methods I am trying to use: Python API call (UpgradeToSessionToken) or manually constructing HTTP request with urllib2.
According to the 2.0 documentation here there is a python example set...
Running the sample code
A full working sample client, containing all the sample code shown in this document, is available in the Python client library distribution, under the directory samples/contacts/contacts_example.py.
The sample client performs several operations on contacts to demonstrate the use of the Contacts Data API.
Hopefully it will point you in the right direction.
I had a similar issue recently. Mine got fixed by setting "secure" to "true".
next = 'http://www.coolcalendarsite.com/welcome.pyc'
scope = 'http://www.google.com/calendar/feeds/'
secure = True
session = True
calendar_service = gdata.calendar.service.CalendarService()
There are four different ways to authenticate. Is it really that important for you to use AuthSub? If you can't get AuthSub to work, then consider the ClientLogin approach. I had no trouble getting that to work.

Categories

Resources