コード例 #1
0
// GetToken will make a request to UAA to retrieve a client token using the
// "client_credentials" grant type. A client id and secret are required.
func (cs ClientsService) GetToken(id, secret string) (string, error) {
	resp, err := newNetworkClient(cs.config).MakeRequest(network.Request{
		Method:        "POST",
		Path:          "/oauth/token",
		Authorization: network.NewBasicAuthorization(id, secret),
		Body: network.NewFormRequestBody(url.Values{
			"client_id":  []string{id},
			"grant_type": []string{"client_credentials"},
		}),
		AcceptableStatusCodes: []int{http.StatusOK},
	})
	if err != nil {
		return "", translateError(err)
	}

	var response documents.TokenResponse
	err = json.Unmarshal(resp.Body, &response)
	if err != nil {
		return "", MalformedResponseError{err}
	}

	return response.AccessToken, nil
}
コード例 #2
0
ファイル: client_test.go プロジェクト: dieucao/notifications
						"headers":{
							"Accept":          ["application/json"],
							"Accept-Encoding": ["gzip"],
							"Authorization":   ["Bearer TOKEN"],
							"Content-Length":  ["17"],
							"Content-Type":    ["application/json"],
							"User-Agent":      ["Go 1.1 package http"]
						}
					}`))
				})

				It("includes a basic Authorization header when there is basic authorization", func() {
					requestArgs := network.Request{
						Method:        "GET",
						Path:          "/path",
						Authorization: network.NewBasicAuthorization("username", "password"),
						Body:          network.NewJSONRequestBody(map[string]string{"hello": "world"}),
						AcceptableStatusCodes: []int{http.StatusOK},
					}
					resp, err := client.MakeRequest(requestArgs)
					Expect(err).NotTo(HaveOccurred())
					Expect(resp.Code).To(Equal(http.StatusOK))
					Expect(resp.Body).To(MatchJSON(`{
						"body": "{\"hello\":\"world\"}",
						"headers":{
							"Accept":          ["application/json"],
							"Accept-Encoding": ["gzip"],
							"Authorization":   ["Basic dXNlcm5hbWU6cGFzc3dvcmQ="],
							"Content-Length":  ["17"],
							"Content-Type":    ["application/json"],
							"User-Agent":      ["Go 1.1 package http"]