英文:
Can't get Oauth2 (Twitter) to work - returns invalid token
问题
所以我对Go有点新手,对我的无知请原谅。我正在尝试使用oauth2对Twitter进行简单的REST API调用,以进行“仅应用程序”调用,但我一直收到“无效或过期的令牌”作为错误返回。
有人有设置类似功能的经验吗?
响应是:{"errors":[{"code":89,"message":"Invalid or expired token."}]}
package main
import "fmt"
import "encoding/base64"
import "io/ioutil"
import "time"
import "golang.org/x/oauth2"
func main() {
config := &oauth2.Config{
Endpoint: oauth2.Endpoint{
AuthURL: "https://api.twitter.com/oauth2/token",
TokenURL: "https://api.twitter.com/oauth/request_token",
},
}
accessToken := base64.StdEncoding.EncodeToString([]byte("{Consumer Key (API Key)}:{Consumer Secret (API Secret)}"))
token := &oauth2.Token{
AccessToken: accessToken,
Expiry: time.Now().Add(time.Duration(24)*time.Hour),
}
httpClient := config.Client(oauth2.NoContext, token)
resp, err := httpClient.Get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=google")
if err != nil {
fmt.Printf("Error: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: %s", err)
}
fmt.Printf("Access Token: %s\nToken: %s\nResponse: %s\n", accessToken, token, body)
}
英文:
So I'm a bit of a newbie to Go, so excuse my ignorance. I'm trying to do a simple REST API call to twitter using oauth2 for an "application only" calls, but I keep getting "Invalid or expired token" back as an error.
Anyone have experience with setting something like this up?
Response is: {"errors":[{"code":89,"message":"Invalid or expired token."}]}
package main
import "fmt"
import "encoding/base64"
import "io/ioutil"
import "time"
import "golang.org/x/oauth2"
func main() {
config := &oauth2.Config{
Endpoint: oauth2.Endpoint{
AuthURL: "https://api.twitter.com/oauth2/token",
TokenURL: "https://api.twitter.com/oauth/request_token",
},
}
accessToken := base64.StdEncoding.EncodeToString([]byte("{Consumer Key (API Key)}:{Consumer Secret (API Secret)}"));
token := &oauth2.Token{
AccessToken: accessToken,
Expiry: time.Now().Add(time.Duration(24)*time.Hour)
}
httpClient := config.Client(oauth2.NoContext, token)
resp, err := httpClient.Get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=google")
if (err != nil) {
fmt.Printf("Error: %s", err)
}
defer resp.Body.Close();
body, err := ioutil.ReadAll(resp.Body);
if (err != nil) {
fmt.Printf("Error: %s", err)
}
fmt.Printf("Access Token: %s\nToken: %s\nResponse: %s\n", accessToken, token, body)
}
答案1
得分: 10
所以结果证明我没有利用客户端凭据oauth2包。我成功让它工作了。
希望这对将来的某个人有所帮助:
package main
import "fmt"
import "io/ioutil"
import "golang.org/x/oauth2"
import "golang.org/x/oauth2/clientcredentials"
func main() {
config := &clientcredentials.Config{
ClientID: "{App Key}",
ClientSecret: "{App Secret}",
TokenURL: "https://api.twitter.com/oauth2/token",
}
tok, err := config.Token(oauth2.NoContext)
httpClient := config.Client(oauth2.NoContext)
resp, err := httpClient.Get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=google")
if err != nil {
fmt.Printf("Error: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: %s", err)
}
fmt.Printf("Access Token: %s\nToken: %s\nResponse: %s\n", tok, body)
}
英文:
So turned out I wasn't leveraging the client credentials oauth2 package. I was able to get it to work.
Hope this helps someone in the future:
package main
import "fmt"
import "io/ioutil"
import "golang.org/x/oauth2"
import "golang.org/x/oauth2/clientcredentials"
func main() {
config := &clientcredentials.Config{
ClientID: "{App Key}",
ClientSecret: "{App Secret}",
TokenURL: "https://api.twitter.com/oauth2/token",
}
tok, err := config.Token(oauth2.NoContext)
httpClient := config.Client(oauth2.NoContext)
resp, err := httpClient.Get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=google")
if (err != nil) {
fmt.Printf("Error: %s", err)
}
defer resp.Body.Close();
body, err := ioutil.ReadAll(resp.Body);
if (err != nil) {
fmt.Printf("Error: %s", err)
}
fmt.Printf("Access Token: %s\nToken: %s\nResponse: %s\n", tok, body)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论