App engine urlfetch issue
Im trying to request https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=[http://www.my-website.dk/]&key=[my-key] using urlfetch from app engine but it's not working.
When I access it and hardcode my-url into the request like this: https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.my-website.dk/&key=[my-key] it's working fine, but when I use urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=%s&key=[my-key]", "http://www.my-website.dk") it's not working, I have also tried:
page_content = urlfetch.fetch(
url="https://www.googleapis.com/pagespeedonline/v1/runPagespeed",
payload=params,
method=urlfetch.GET
)
and then serving the parameters in the payload like this:
params = urllib.urlencode({
"url": page.link,
"key": "[my-key]"
})
but the result is the same, it's not working and the service gives me HT开发者_如何学PythonTP status code 400. I also tried adding urlfetch.fetch(u("http://...", page.link) but the result is the same.
I edited the code based on the reply from systempuntoout incase any one should run into the same problem:
params = urllib.urlencode({
"url": page.link,
"key": "AIzaSyAFpm6W_OmjQl33JC98mAPkvrdGmrR0i4Y"
})
page_content = urlfetch.fetch("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?%s" % params)
First, the urlfetch
call has an error because you are passing two parameters to the function.
You should use the %
between the two strings to pass just one url parameter to the function.
urlfetch.fetch("https://www.goo..e/v1/runPagespeed?url=%s&key=[my-key]" %
"http://www.my-website.dk")
Then, have you tried to urlencode the second url?
import urllib
your_url = {'url': 'http://www.my-website.dk/&key=[my-key]'}
urlfetch.fetch("https://www.g../v1/runPagespeed?%s" % urllib.urlencode(your_url))
payload is only applicable for POST/PUT requests. For GET, your params need to be part of the URL
精彩评论