英文:
GOLANG HTTP Basic-Auth with Google App Engine URLFetch
问题
你好!以下是你要翻译的内容:
如何在Go中为urlfetch
客户端添加Authorization
头?
有一个类似的问题在java
和python
中有答案,但是在Go
中没有。
英文:
How can I add an Authorization
header to urlfetch
client with Go?
There is a similar question answered for java
and python
, but not Go
.
答案1
得分: 2
urlfetch.Client(ctx)
返回一个 HTTP 客户端(http://godoc.org/google.golang.org/appengine/urlfetch#Client)。
http.Client
有 Get
、Post
等方法... 它还有 Do
方法,你可以将任意请求传递给它。使用 http.NewRequest
创建一个请求:
req, err := http.NewRequest("GET", "http://www.google.com", nil)
然后你可以像这样添加一个头部:
req.Header.Set("Authorization", "whatever")
并调用 Do
:
res, err := client.Do(req)
英文:
urlfetch.Client(ctx)
returns an HTTP client (http://godoc.org/google.golang.org/appengine/urlfetch#Client)
The http.Client
has methods for Get
, Post
, etc... It also has Do
which you can hand an arbitrary request. Create a request using http.NewRequest
:
req, err := http.NewRequest("GET", "http://www.google.com", nil)
Then you can add a header like this:
req.Header.Set("Authorization", "whatever")
And call Do
:
res, err := client.Do(req)
答案2
得分: 2
我是你的中文翻译助手,以下是翻译好的内容:
我刚开始学习Go语言,所以请原谅这段代码的丑陋/格式错误/错误之处。
我一直在努力解决这个问题,并在appengine上遇到了相同的问题。
上面Caleb的答案对我很有帮助。我只是在那个答案的基础上添加了一些细节,以帮助可能遇到类似问题的人。
这是我的导入语句的样子:
import (
"appengine"
"appengine/urlfetch"
"bytes"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"net/url"
)
这是一个函数,用于接收传入的身份验证回调请求,然后回复一个向身份验证服务器请求访问令牌的请求。在这种情况下,是针对Fitbit的,需要设置一个带有额外信息的身份验证头部为"Basic"。我无法弄清楚如何使用默认的Oauth2库来实现这一点,因为它似乎不容易允许更改请求头部。
正如你所看到的,我们从传入请求的上下文(r)中获取了http.Client。
然后我们打包并发送一个带有一些授权信息的请求。
在我们收到响应后,将结果打印到浏览器上。
希望对你有所帮助!
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
data := url.Values{}
data.Set("client_id", "231343")
data.Add("grant_type", "authorization_code")
data.Add("redirect_uri", "http://localhost:8080/callmebacklaterok")
data.Add("code", "authcode123132")
encodedData := data.Encode()
c := appengine.NewContext(r)
client := urlfetch.Client(c)
urlStr := "https://api.fitbit.com/oauth2/token"
req, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(encodedData))
req.Header.Add("Authorization", "Basic RmFuY3kgbWVldGluZyB5b3UgaGVyZSEg")
resp, _ := client.Do(req)
defer resp.Body.Close()
fmt.Fprint(w, resp)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
var bodydata interface{}
err = json.Unmarshal(body, &bodydata)
if err != nil {
panic(err.Error())
}
fmt.Fprint(w, bodydata)
}
英文:
I'm new to Go, please excuse this code for being ugly/malformed/just plain wrong.
I've been working my way though this and ran across the same problem on appengine.
@Caleb's answer above was a big help. I've just added some detail to that to help someone who might come across a similar problem.
Here's what my import statement looks like:
Import {
"appengine"
"appengine/urlfetch"
"bytes"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"net/url"
}
This is a function that receives and incoming authentication callback request, then replies with a request for an access token from the authentication server. In this case, fitbit, which needs an Authentication header set to "Basic" with some extra information. I couldn't figure out how to do this with the stock Oauth2 library, which doesn't seem to easily allow changing the request headers.
As you can see we the context of the incoming request (r). From that context, we get the http.Client from urlfetch.
Then we pack up and send a request back, with some authorization information.
After we get a response we print the results to the browser.
Hope this helps!
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
data := url.Values{}
data.Set("client_id", "231343")
data.Add("grant_type", "authorization_code")
data.Add("redirect_uri", "http://localhost:8080/callmebacklaterok")
data.Add("code", "authcode123132")
encodedData := data.Encode()
c := appengine.NewContext(r)
client := urlfetch.Client(c)
urlStr := "https://api.fitbit.com/oauth2/token"
req, _ := http.NewRequest("POST", urlStr,bytes.NewBufferString(encodedData))
req.Header.Add("Authorization", "Basic RmFuY3kgbWVldGluZyB5b3UgaGVyZSEg")
resp, _ := client.Do(req)
defer resp.Body.Close()
fmt.Fprint(w, resp)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
var bodydata interface{}
err = json.Unmarshal(body, &bodydata)
if err != nil {
panic(err.Error())
}
fmt.Fprint(w, bodydata)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论