Wednesday, 15 February 2012

google app engine - Runtime error when attempting to read *http.Response Body, having used urlfetch.Transport -



google app engine - Runtime error when attempting to read *http.Response Body, having used urlfetch.Transport -

app engine not allow utilize of defaultclient, providing urlfetch service instead. next minimal illustration deploys , works pretty much expected:

package app import ( "fmt" "net/http" "appengine" "appengine/urlfetch" "code.google.com/p/goauth2/oauth" ) func init () { http.handlefunc("/", home) } func home(w http.responsewriter, r *http.request) { c := appengine.newcontext(r) config := &oauth.config{ clientid: "<redacted>", clientsecret: "<redacted>", scope: "email", authurl: "https://www.facebook.com/dialog/oauth", tokenurl: "https://graph.facebook.com/oauth/access_token", redirecturl: "http://example.com/", } code := r.formvalue("code") if code == "" { http.redirect(w, r, config.authcodeurl("foo"), http.statusfound) } t := &oauth.transport{config: config, transport: &urlfetch.transport{context: c}} tok, _ := t.exchange(code) graphresponse, _ := t.client().get("https://graph.facebook.com/me") fmt.fprintf(w, "<pre>%s<br />%s</pre>", tok, graphresponse) }

with right clientid, clientsecret , redirecturl, produces next output (edited brevity):

&{aaadtwgsq5<snip>kmdjh5vkwzdzd 0001-01-01 00:00:00 +0000 utc} &{200 ok %!s(int=200) http/1.1 %!s(int=1) %!s(int=1) map[connection:[keep-alive] access-control-allow-origin:[*] <snip> content-type:[text/javascript; charset=utf-8] date:[wed, 06 feb 2013 12:06:45 gmt] x-google-cache-control:[remote-fetch] cache-control:[private, no-cache, no-store, must-revalidate] pragma:[no-cache] x-fb-rev:[729873] via:[http/1.1 gwa] expires:[sat, 01 jan 2000 00:00:00 gmt]] %!s(*urlfetch.bodyreader=&{[123 34 105 100 <big snip> 48 48 34 125] false false}) %!s(int64=306) [] %!s(bool=true) map[] %!s(*http.request=&{get 0xf840087230 http/1.1 1 1 map[authorization:[bearer aaadtwgsq5nsbac4yt0x1shzajatodoix0tzcb tytjxfc4eseqcjpdi3remkhbujzcx4fiklo1ujmpjxhjzcfgfcojlfu7uvehkmdjh5vkwzdzd]] 0 [] false graph.facebook.com map[] map[] })}

it seems i'm consistently getting *http.response back, expect able read response body. however, mention of body--for illustration with:

defer graphresponse.body.close()

compiles, deploys, results in next runtime error:

panic: runtime error: invalid memory address or nil pointer dereference runtime.panic go/src/pkg/runtime/proc.c:1442 runtime.panicstring go/src/pkg/runtime/runtime.c:128 runtime.sigpanic go/src/pkg/runtime/thread_linux.c:199 app.home app/app.go:33 net/http.handlerfunc.servehttp go/src/pkg/net/http/server.go:704 net/http.(*servemux).servehttp go/src/pkg/net/http/server.go:942 appengine_internal.executerequestsafely go/src/pkg/appengine_internal/api_prod.go:240 appengine_internal.(*server).handlerequest go/src/pkg/appengine_internal/api_prod.go:190 reflect.value.call go/src/pkg/reflect/value.go:526 reflect.value.call go/src/pkg/reflect/value.go:334 _ _.go:316 runtime.goexit go/src/pkg/runtime/proc.c:270

what missing? because of utilize of urlfetch rather defaultclient?

okay, of course of study own silly fault can see how others fall same trap here's solution, prompted andrew gerrand , kyle lemons in this google-appengine-go topic (thanks guys).

first of all, wasn't handling requests favicon.ico. can taken care of next instructions here , adding section app.yaml:

- url: /favicon\.ico static_files: images/favicon.ico upload: images/favicon\.ico

this fixed panics on favicon requests, not panics on requests '/'. problem was, i'd assumed http.redirect ends handler execution @ point. doesn't. needed either homecoming statement next redirect, or else clause:

code := r.formvalue("code") if code == "" { http.redirect(w, r, config.authcodeurl("foo"), http.statusfound) } else { t := &oauth.transport{config: config, transport: &urlfetch.transport{context: c}} tok, _ := t.exchange(code) fmt.fprintf(w, "%s", tok.accesstoken) // ... }

i don't recommend ignoring error of course of study deploys , runs expected, producing valid token.

google-app-engine facebook-graph-api go

No comments:

Post a Comment