无法获取Oauth2(Twitter)的工作 – 返回无效令牌

huangapple go评论93阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2016年1月14日 07:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/34779108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定