html appengine redirect URL

2019-08-11 13:48发布

I want to redirect a user to the next URL after authenticating on a third party web page. In the redirect URL I want it to contain some info about the user, i.e. session id. For example my redirect URL will be www.z.appspot.com/?session=x/ when the user returns to my page I want to parse some of their details from the new URL, for example www.z.appspot.com/?access_code=x/?token=y&user_id=u but when trying to parse out the information it is unable to see the token in the URL as it takes it to be part of session. I tried using & but still no joy

3条回答
太酷不给撩
2楼-- · 2019-08-11 14:36

Your question is a bit confusing, but is there a reason your other service can not redirect to: www.z.appspot.com/?access_code=x/&token=y&user_id=u? That should be correctly parsed:

import urlparse
import cgi

url = "www.z.appspot.com/?access_code=x/&token=y&user_id=u"
query = urlparse.urlparse(url).query
print cgi.parse_qs(query)
# output: {'access_code': ['x/'], 'token': ['y'], 'user_id': ['u']}

If you can not change the format of the url, you could do something like:

import urlparse
import cgi

url = "www.z.appspot.com/?access_code=x/?token=y&user_id=u"
query = urlparse.urlparse(url).query
access_code, query = query.split('?', 1)
access_code = access_code.split('=', 1)[1]
print access_code
# output: 'x/'
print cgi.parse_qs(query)
# output: {'token': ['y'], 'user_id': ['u']}

Or, if you're just trying to ask how to embed parameters in the URL they will be redirected to, see Python's urlencode.

Update:

import urllib
encoded = urllib.urlencode({'app_id': 11,
                            'next': 'http://localhost:8086/?access_tok­en=%s' % (access_token,)})
hunch_url = 'http://hunch.com/authorize/v1/?%s' % (encoded,)

Then pass hunch_url to your template.

查看更多
Fickle 薄情
3楼-- · 2019-08-11 14:44

It looks like the service you're redirecting to doesn't compose query parameters in the continue URL correctly. The easiest way around this would be (as I suggested in your original question) to send the parameter as part of the path, rather than the query string - that is, use URLs of the form:

http://www.yourapp.com/continue/1234

Where 1234 is the user's session ID or user ID.

查看更多
Animai°情兽
4楼-- · 2019-08-11 14:46

You want a module called cgi, and a function called FieldStorage. Do it like this:

import cgi

url_parms = cgi.FieldStorage()
session = url_parms.getvalue("session","default")

You can do this for any of the URL parameters. Replace "default" with whatever you want the default value to be (like an empty string).

查看更多
登录 后发表回答