I'm just wanting to return a JSON object, but HTTP information is being printed below it. I'm using Google App Engine and https://github.com/simplegeo/python-oauth2
#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson as json
import oauth2 as oauth
import cgi
class MainHandler(webapp.RequestHandler):
def get(self):
consumer = oauth.Consumer(key="xxx",
secret="xxx")
request_token_url = "xxx"
client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(cgi.parse_qsl(content))
print
print json.dumps({"oauth_token": request_token['oauth_token'], "oauth_token_secret": request_token['oauth_token_secret']})
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Add the proper Content-Type and switch to self.response.out.write
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(data)
Instead of print use self.response:
self.response.out.write("Some Text")
I suppose that the RequestHandler automatically prints a default HTTP header and sends it if nothing is written to the response.out stream.
If you only want to send JSON data you can set the "Content-Type" header information to "application/json".
Related
New to Flask and API development, but I'm trying to figure out what's wrong here.
This is my code in my koho-flask.py
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
import new_model_zero_shot as model
app = Flask(__name__)
api = Api(app)
#create new user_palette object
user = model.user_palette(model.cold_start(), 1)
dish_list = model.dish_list
#user.recommend_meal(['chicken', 'rice'], query_type = ['our_dbs', dish_list], name_csv = None)
# argument parsing
parser = reqparse.RequestParser()
parser.add_argument('query')
# we already have a way to package the output to user
class GetRecipes(Resource):
def get(self):
# use parser and find the user's query
args = parser.parse_args()
user_query = args['query']
recs = (user.recommend_meal(user_query,
query_type = ['our_dbs', dish_list], name_csv = None))
return recs
class Hello(Resource):
def get(self):
return {'data': "Hello wassup!"}
#recipe search endpoint
api.add_resource(GetRecipes, '/')
api.add_resource(Hello, '/')
#%%
if __name__ == '__main__':
app.run(debug=False)
When I run app.run(debug=False), the server starts.
After, I open a test.py file in the same directory and run this.
import requests
url = 'http://127.0.0.1:5000/'
params ={'query': 'chicken'}
response = requests.get(url, params)
a = response.json()
My "a" variable returns a dictionary with the value "The browser (or proxy) sent a request that this server could not understand". This happens whether I test out my dummy "Hello" endpoint or my real "GetRecipes" endpoint.
What am I doing incorrectly?
Im trying to create a script, either by Python or PHP, that can receive JSON from Dialogflow webhooks.
The data value will be from resolvedQuery.
I want to parse the data then send to the below URL:
https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?text=***JSON GOES HERE ***&deviceId=18c972b753ad&apikey=5b48aed7
The data from resolvedQuery needs to be sent to the about URL where *JSON GOES HERE * is.
Here is the Python code I have been trying:
from flask import Flask
from flask import request
from flask import make_response
import json
import logging as l
import requests
app = Flask(__name__)
#app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
l.info("reached the hello() module...")
return 'Hello Beautiful World!\n'
#app.route('/apiai', methods=['POST'])
def apiai_response():
requests_session = requests.session()
requests_session.headers.update({'Content-Type': 'application/json'})
requests_session.headers.update({'charset':'utf-8'})
post_data = '[{ "resolvedQuery": "" }]'
requests_response = requests_session.post(url="https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?text=***JSON GOES HERE ***&deviceId=18c972b753ad&apikey=5b48aed7", data=post_data)
print requests_response.content
#app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, nothing at this URL.', 404
I would try something like this:
import json
import requests
json_string = json.dumps({"hello": "world"})
these_params = {"text": json_string,
"deviceId": "18c972b753ad&apikey=5b48aed7"}
this_url = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush"
r.get(url=this_url, params=these_params)
notice it is a get request and not a post request
It is a matter of opinion however using url encoded params on a GET request differ in how stuff gets read. As far as the data field is concerned on a post request, the handling is a bit different.
On a get request it will look something like part of the url as :
text=%7B%22hello%22%3A%22world%22%7D
Where as a post request the server will try to read in a application/json encoded body which expects the "data" to be valid json text.
also note:
How to urlencode a querystring in Python?
I have two servers where one is trying to get a file from the other. I am using Flask get requests to send simple data back and forth (strings, lists, JSON objects, etc.).
I also know how to send just a file, but I need to send an error code with my data.
I'm using something along the following lines:
Server 1:
req = requests.post('https://www.otherserver.com/_download_file', data = {'filename':filename})
Server 2:
#app.route('/_download_file', methods = ['POST'])
def download_file():
filename = requests.form.get('filename')
file_data = codecs.open(filename, 'rb').read()
return file_data
Server 1:
with codecs.open('new_file.xyz', 'w') as f:
f.write(req.content)
...all of which works fine. However, I want to send an error code variable along with file_data so that Server 1 knows the status (and not the HTTP status, but an internal status code).
Any help is appreciated.
One solution that comes to my mind is to use a custom HTTP header.
Here is an example server and client implementation.
Of course, you are free to change the name and the value of the custom header as you need.
server
from flask import Flask, send_from_directory
app = Flask(__name__)
#app.route('/', methods=['POST'])
def index():
response = send_from_directory(directory='your-directory', filename='your-file-name')
response.headers['my-custom-header'] = 'my-custom-status-0'
return response
if __name__ == '__main__':
app.run(debug=True)
client
import requests
r = requests.post(url)
status = r.headers['my-custom-header']
# do what you want with status
UPDATE
Here is another version of the server based on your implementation
import codecs
from flask import Flask, request, make_response
app = Flask(__name__)
#app.route('/', methods=['POST'])
def index():
filename = request.form.get('filename')
file_data = codecs.open(filename, 'rb').read()
response = make_response()
response.headers['my-custom-header'] = 'my-custom-status-0'
response.data = file_data
return response
if __name__ == '__main__':
app.run(debug=True)
Been working on an appengine app lately. I would like to parse json data contained in requests to the app. How do I use the request object of the requesthandler class to achieve this?
Below is a snippet of the code to show what I want to achieve:
import cgi
import webapp2
import datamethods
from google.appengine.ext.webapp.util import run_wsgi_app
class adduser(webapp2.RequestHandler):
def get(self):
# Get the phone number from json data in request.
userphone = self.request.get("phone")
# Get the name from json data in request.
name = self.request.get("name")
app = webapp2.WSGIApplication([
('/adduser', adduser),
('/sign', updatestatus),
('/login',login)
], debug=True)
def main():
run_wsgi_app(app)
if __name__ == "__main__":
main()
You have to parse the incoming json string in an object. After this you can access the attributes.
import json # Now you can import json instead of simplejson
....
jsonstring = self.request.body
jsonobject = json.loads(jsonstring)
import cgi
import webapp2
import datamethods
from google.appengine.ext.webapp.util import run_wsgi_app
class adduser(webapp2.RequestHandler):
def get(self):
items = []
response = { }
userphone = self.request.get("phone")
name = self.request.get("name")
items.append({'userphone': userphone , 'name':name})
response['userInformation'] = items
return response #return json data
app = webapp2.WSGIApplication([
('/adduser', adduser),
('/sign', updatestatus),
('/login',login)
], debug=True)
def main():
run_wsgi_app(app)
if __name__ == "__main__":
main()
I am trying to create generic class in python which will do all the oAuth process and then will allow to retrieve data from any oAuth supporting service (for example Twitter,LinkedIn).
Edited:
I have customer key and secret and access token key and secret,when I try to request any resource request I get the following error:
{"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/retweeted_by_me.json}'
any idea why?
My Code is:
import httplib
import time
import oauth as oauth
# settings for the local test consumer
SERVER = 'api.twitter.com'
RESOURCE_URL = 'https://api.twitter.com/1/statuses/retweeted_by_me.json'
CONSUMER_KEY = 'MY_CUSTOMER_KEY'
CONSUMER_SECRET = 'MY_CUSTOMER_SECRET'
ACCESS_TOKEN_KEY = 'MY_ACCESS_TOKEN_KEY'
ACCESS_TOKEN_SECRET = 'MY_ACCESS_TOKEN_SECRET'
# example client using httplib with headers
class SimpleOAuthClient(oauth.OAuthClient):
def __init__(self, server):
self.server = server
self.connection = httplib.HTTPSConnection(self.server)
def access_resource(self, oauth_request):
# via post body
# -> some protected resources
self.connection.request(oauth_request.http_method, RESOURCE_URL)
response = self.connection.getresponse()
return response.read()
def run_example2():
print '** OAuth Python Library Example **'
client = SimpleOAuthClient(SERVER, )
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
pause()
# access some protected resources
print '* Access protected resources ...'
pause()
token = oauth.OAuthToken('ACCESS_TOKEN_KEY', 'ACCESS_TOKEN_SECRET')
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='GET', http_url=RESOURCE_URL)
oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
print 'REQUEST (via post body)'
print 'parameters: %s' % str(oauth_request.parameters)
pause()
params = client.access_resource(oauth_request)
print 'GOT'
print 'non-oauth parameters: %s' % params
pause()
def pause():
print ''
time.sleep(1)
if __name__ == '__main__':
run_example2()
print 'Done.'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authenticate'
This is the incorrect URL to use for OAuth. If you look at Twitter's 3-legged OAuth documentation, they state "The GET oauth/authorize endpoint is used instead of /oauth/authenticate". Change the URL to "https://api.twitter.com/oauth/authorize" and try again.
I managed to fix it by changing self.connection.request(oauth_request.http_method, RESOURCE_URL)
to self.connection.request(oauth_request.http_method, oauth_request.to_url())
Notice that will will work only if oauth_request.http_method is GET