N
N
Newmo2019-10-16 11:12:31
go
Newmo, 2019-10-16 11:12:31

How to pass authorization and send Get to yandex disk api?

There is an Api token, if you add it to the header of a regular get request, net / http swears at the wrong header.
How to log in and send a request to Yandex.Disk api?
I do it like this:

client := &http.Client{}
  req, err := http.NewRequest(
    "GET", "https://cloud-api.yandex.net/v1/disk", nil,
  )
  req.Header.Add("Authorization: OAuth", "Сам токен")
  resp, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer resp.Body.Close()
  io.Copy(os.Stdout, resp.Body)

I get an error accordingly:
Get https://cloud-api.yandex.net/v1/disk: net/http: invalid header field name "Authorization: OAuth"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Newmo, 2019-10-16
@Newmo

client := &http.Client{}
  req, err := http.NewRequest(
    "GET", "https://cloud-api.yandex.net/v1/disk", nil,
  )
  req.Header.Add("Authorization", "токен")
  resp, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer resp.Body.Close()
  io.Copy(os.Stdout, resp.Body)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question