Seu código possui uma diversidade de problemas!
O primeiro é que você está usando t
com um formato incorreto. O formato esperado para oauth_timestamp é timestamp em segundos. Veja a diferença:
//erro #1
t := time.Now().Format("20060102150405")
fmt.Println("time: ", t)
//correto #1
t_ := fmt.Sprintf("%d", time.Now().Unix())
fmt.Println("time: ", t_)
Veja no playground: https://play.golang.org/p/L74MzEHkVwS
Outro erro é que você não está seguindo os passos para a construção da coleção dos parametros. Na documentação diz:
The process to build the string [for Collecting parameters] is very
specific:
- Percent encode every key and value that will be signed.
- Sort the list of parameters alphabetically 1 by encoded key [2].
- For each key/value pair:
- Append the encoded key to the output string.
- Append the "=" character to the output string.
- Append the encoded value to the output string.
- If there are more key/value pairs remaining, append a ‘&’ character to
the output string.
Observe alguns pontos importantes neste passo:
- Você deve fazer o encode na chave e no valor;
- Você deve ordenar a chave
Isto quer dizer que ao invés de você usar um url.Values()
você deveria ter feito algo como:
values := make(map[string]string)
values["oauth_nonce"] = auth.ANonce
values["oauth_callback"] = auth.Path
values["oauth_signature_method"] = "HMAC-SHA1"
values["oauth_timestamp"] = t
values["oauth_consumer_key"] = auth.ClientID
values["oauth_version"] = "1.0" //include here
//encode each key/value, sort key and build string
params := sortedParams(values)
De posse dos parâmetros você deve produzir a base para a assinatura. Neste ponto você comete mais dois erros:
- Você não codifica a url da requisição e
- Você não codifica os parâmetros coletados.
Na documentação diz:
To encode the HTTP method, base URL, and parameter string into a
single string:
- Convert the HTTP Method to uppercase and set the output string equal
to this value.
- Append the ‘&’ character to the output string.
- Percent encode the URL and append it to the output string.
- Append the ‘&’ character to the output string.
- Percent encode the parameter string and append it to the output
string.
Make sure to percent encode the parameter string! The signature base
string should contain exactly 2 ampersand ‘&’ characters.
Assim sendo você deveria fazer:
//build signature base
baseString := fmt.Sprintf("POST&%s&%s",
url.QueryEscape(auth.RequestToken), //here is request token url
url.QueryEscape(params))
Antes de fazer a requisição você comete mais dois erros, você remove oauth_version
dos parâmetros iniciais e não codifica a assinatura. E este passo é feito tanto na inclusão da assinatura aos parâmetros quando no header do request. Veja:
The signing key is simply the percent encoded consumer secret,
followed by an ampersand character ‘&’[...]
Dessa forma você deverá fazer algo assim:
values["oauth_signature"] = url.QueryEscape(signature)
header := fmt.Sprintf(`OAuth oauth_nonce="%s", ...`,
url.QueryEscape(auth.ANonce),
url.QueryEscape(auth.Path),
url.QueryEscape(t),
url.QueryEscape(auth.ClientID),
url.QueryEscape(signature))
Removendo esses erros você irá resolver seu problema!
Link para as referências:
Eu testei seu código e deixei no playground para um melhor entendimento.
Espero tê-lo ajudado!