urllib2 error on appengine - google-app-engine

The following works locally but not when deployed:
import urllib
import urllib2
import simplejson as json
url = 'https://www.googleapis.com/rpc'
requests = [{
'method': 'freebase.text.get',
'apiVersion': 'v1',
'params': {
'id': ['en','bob_dylan']
}
},{
'method': 'freebase.text.get',
'apiVersion': 'v1',
'params': {
'id': ['en','blade_runner']
}
}]
headers = { 'Content-Type': 'application/json' }
req = urllib2.Request(url, json.dumps(requests), headers)
response = urllib2.urlopen(req)
print response.read()
When deployed it throws the following error:
class 'urllib2.HTTPError': HTTP Error 404: Not Found
Traceback (most recent call last):
File "/base/data/home/apps/s~34thquote/1.359254037177435492/test.py", line 38, in <module>
response = urllib2.urlopen(req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 387, in open
response = meth(req, response)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 425, in error
return self._call_chain(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 506, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
It could be related to this problem: freebase api error on deployment to appengine: DownloadError: ApplicationError: 2 in which case adding an api key to the request could fix it. Though my attempt to add a key (using urlfetch) resulted in an error:
{u'code': 100, u'message': u'Invalid API Key (Key not found)', u'stat': u'fail'}.
I registered the freebase service and added the Simple API key for browser apps to the query: '&key=apikeystringhere'. I've added the key issue as a separate question: Setup api key for freebase queries from appengine

Why not use the urlfetch API rather than urllib2?

I have it working now. It was not working on the remote because I needed a key. My previous attempt to add the key failed because i was using the deprecated http://api.freebase.com API when I should have been using: https://www.googleapis.com/freebase/v1/search?q=bob&key=

Related

taskqueue is not working google app engine

File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/django/views/generic/base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "/workspace/apps/account/views.py", line 235, in post
taskqueue.add(queue_name='create-waybill', url = reverse('_api:createWaybill') , params = {'key':url_key})
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/appengine/api/taskqueue/taskqueue.py", line 575, in create_rpc
return apiproxy_stub_map.UserRPC('taskqueue', deadline, callback)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/appengine/api/apiproxy_stub_map.py", line 444, in __init__
self.__rpc = CreateRPC(service, stubmap)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/appengine/api/apiproxy_stub_map.py", line 69, in CreateRPC
assert stub, 'No api proxy found for service "%s"' % service
AssertionError: No api proxy found for service "taskqueue"
I am working on google app engine and try to run taskqueue and getting error
i am using django python3
from google.appengine.api import taskqueue
taskqueue.Task(name='create-waybill', url='/_api/createWaybill',params = {'key':url_key}).add()
just this one
and
path ('createWaybill', csrf_exempt(views.CreateWaybill.as_view()), name='createWaybill'),
To use the bundled services in Python 3, you first have to enable them.
See documentation from Google for doing that

prawcore.exceptions.NotFound: received 404 HTTP response

Hey guys I'm having trouble figuring this out:
Traceback (most recent call last):
File "SOLIS.py", line 62, in <module>
com_df1 = scraper.comments(sub_id_list,keywords1,keywords2)
File "/Users/CarranzaEE/Desktop/CDS 491:DEXIS/Reddit_SOLIS/Reddit_SOLIS.py", line 252, in comments
if((comment.author is None) or (hasattr(comment.author, 'id') != True)):
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/praw/models/reddit/base.py", line 34, in __getattr__
self._fetch()
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/praw/models/reddit/redditor.py", line 171, in _fetch
data = self._fetch_data()
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/praw/models/reddit/redditor.py", line 168, in _fetch_data
return self._reddit.request("GET", path, params)
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/praw/reddit.py", line 765, in request
json=json,
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/prawcore/sessions.py", line 339, in request
url=url,
File "/Users/CarranzaEE/anaconda3/lib/python3.7/site-packages/prawcore/sessions.py", line 265, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.NotFound: received 404 HTTP response
This error happens when I try to use author (an instance of Redditor) to try and call author.id. PRAW documents also say that Shadowbanned accounts are treated the same as non-existent accounts, meaning that they will not have any attributes, and that will only return name and is_suspended.
But is_suspended is no longer an attribute provided by reddit api. So I'm having trouble trying to handle cases where the account is a shawdowbanned account. Any tips or ideas?

googleapiclient.errors.HttpError: HttpError 500 'backend error' when requesting https://www.googleapis.com/gmail/v1/users/me/watch

When running the following minimal code to execute the GMail API watch() method. A 'Backend Error' occurs. This is true both when running on a client machine, and when run directly on Google Cloud as a Cloud Function.
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ["https://mail.google.com/"]
SERVICE_ACCOUNT_FILE = '<JSON KEY FILE REMOVED>'
TARGET='<GSUITE DOMAIN USER EMAIL ADDRESS>'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
credentials_delegated = credentials.with_subject(TARGET)
service = googleapiclient.discovery.build('gmail', 'v1', credentials=credentials_delegated)
val = { 'topicName': '<TOPIC NAME COPIED FROM GCLOUD PUB/SUB>' }
watch_resp = service.users().watch(userId='me', body=val).execute()
print(watch_resp)
Other GMail APIs such as service.users().getProfile(userId='me').execute() work as expected. This error occurs after ensuring that the service account and the delegated user have the pub/sub publisher role and that the topic name is correct and already created. The exact output of the error is as follows:
Traceback (most recent call last):
File "test.py", line 12, in <module>
watch_resp = service.users().watch(userId='me', body=val).execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/gmail/v1/users/me/watch?alt=json returned "Backend Error">

App Engine dev_appserver.py with WebPack return 404 from Google Storage Local API

I'm developing an app using Google App Engine Standard Environment with Python and frontend with Webpack. The application store photos in Google Storage and display them. When I deploy the app to App Engine everything works fine.
When I try to run the local dev_appserver.py and Webpack I get the exception bellow when trying to read from Google Storage API when accessing the app via Webpack DevServer port (3001 in my case). If build the Webpack static output and do the exact same request with dev_appserver.py port (8080) I get the photo back.
[appengine] ERROR 2017-08-26 20:19:18,786 photo_storage.py:31] Fail to read photo file
[appengine] Traceback (most recent call last):
[appengine] File "c:\myapp\server\api\photo_storage.py", line 20, in read_photo_from_storage
[appengine] file_stat = gcs.stat(filename)
[appengine] File "c:\myapp\server\lib\cloudstorage\cloudstorage_api.py", line 151, in stat
[appengine] body=content)
[appengine] File "c:\myapp\server\lib\cloudstorage\errors.py", line 132, in check_status
[appengine] raise NotFoundError(msg)
[appengine] NotFoundError: Expect status [200] from Google Storage. But got status 404.
[appengine] Path: '/app_default_bucket/pics/185804764220139124118/395ebb68_main.png'.
[appengine] Request headers: None.
[appengine] Response headers: {'date': 'Sat, 26 Aug 2017 20:19:18 GMT', 'connection': 'keep-alive', 'content-type': 'text/html; charset=utf-8', 'content-security-policy': "default-src 'self'", 'x-content-type-options': 'nosniff', 'x-powered-by': 'Express', 'content-length': '211'}.
[appengine] Body: ''.
[appengine] Extra info: None.
This is the code that reads and write the files:
import logging
import os
import logging
import urllib
from google.appengine.api import app_identity
import cloudstorage as gcs
from models import Photo
def read_photo_from_storage(photo, label, response):
bucket_name = os.environ.get('BUCKET_NAME',
app_identity.get_default_gcs_bucket_name())
filename = format_photo_file_name(bucket_name, photo.created_by_user_id, photo.crc32c, label)
try:
file_stat = gcs.stat(filename)
gcs_file = gcs.open(filename)
response.headers['Content-Type'] = file_stat.content_type
response.headers['Cache-Control'] = 'private, max-age=31536000' # cache for upto 1 year
response.headers['ETag'] = file_stat.etag
response.write(gcs_file.read())
gcs_file.close()
except gcs.NotFoundError:
logging.exception("Fail to read photo file")
response.status = 404
response.write('photo file not found')
def write_photo_to_storage(user_id, checksum, label, file_type, image_content):
bucket_name = os.environ.get('BUCKET_NAME',
app_identity.get_default_gcs_bucket_name())
filename = format_photo_file_name(bucket_name, user_id, checksum, label)
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open(filename,
'w',
content_type=file_type,
options={'x-goog-meta-crc32c': "{0:x}".format(checksum)},
retry_params=write_retry_params)
gcs_file.write(image_content)
gcs_file.close()
def format_photo_file_name(bucket_name, user_id, checksum, label):
return urllib.quote("/{0}/pics/{1}/{2:x}_{3}.png".format(bucket_name, user_id, checksum, label))

angularjs doesnt POST to my django based apis - giving CORS error

This has been plaguing me for quite a while now. I've set up a basic set of apis in Django and am building an angularJS based front end. However for some weird reason the following code results in a CORS error.
var req = {
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
withCredentials: true,
param: data,
data: data
}
return $http(req).then(
function(response){
callbackSuccess(response);
},
function(response){
callbackError(response);
}
);
I found out two things that when I do a post an OPTIONS request is made and secondly none of what I post is even posted. I try to out put the request.POST contents and its empty.
Internal Server Error: /api/users/auth/
Traceback (most recent call last):
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/ali/Projects/api.project.local/project/api/views.py", line 50, in loginUser
emailAddress = request.POST["emailAddress"]
File "/home/ali/eb-virt/local/lib/python2.7/site-packages/django/utils/datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'emailAddress'"
[21/Nov/2016 07:56:59] "OPTIONS /api/users/auth/ HTTP/1.1" 500 81083
And this is my server code:
def loginUser(request):
emailAddress = ''
password = ''
emailAddress = request.POST["emailAddress"]
password = request.POST["password"]
data = auth.login(emailAddress, password)
return data
I've installed the django-cors-headers and followed all the instructions to the core but its still not working...
The problem is in Line :
emailAddress = request.POST["emailAddress"]
In the place of this line,use this code:
def loginUser(request):
if request.method == 'POST':
emailAddress = request.POST.get('emailAddress')
password = request.POST.get('password')
data = auth.login(emailAddress, password)
return data
request.POST["emailAddress"] will raise a KeyError exception if 'emailAddress' is not in request.POST.
While request.POST.get('emailAddress') will return None if 'emailAddress' is not in request.POST.
OPTION request is doing by your browser for a safety whether it can send POST request(in case of cross domain). It should return a 200 with some headers(will take care of cors-headers middleware if you added it).
So In server, accept POST request this way(assume function based views).
if request.method == 'POST':
#access POST variables here.
Found the issue - was a total noob mistake.
My version of Django in my virtual environment was 1.9 whereas I had generated the project from a version 1.10 so in the settings the MIDDLEWARE option had to be named as MIDDLEWARE_CLASSES - silly mistake had me stuck for two days - thanks everyone for all your answers :)

Resources