Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go Can';t使Oauth2(Twitter)工作-返回无效令牌_Go_Oauth 2.0_Twitter Oauth - Fatal编程技术网

Go Can';t使Oauth2(Twitter)工作-返回无效令牌

Go Can';t使Oauth2(Twitter)工作-返回无效令牌,go,oauth-2.0,twitter-oauth,Go,Oauth 2.0,Twitter Oauth,所以我是个新手,请原谅我的无知。我试图使用oauth2对twitter进行一个简单的RESTAPI调用,用于“仅应用程序”调用,但我一直将“无效或过期令牌”作为错误返回 有没有人有过这样的经验 响应为:{“错误”:[{“代码”:89,“消息”:“无效或过期的令牌。”}]} 结果证明我没有利用客户端凭证oauth2包。我能让它工作 希望这对将来的人有所帮助: package main import "fmt" import "io/ioutil" import "golang.org/x/oau

所以我是个新手,请原谅我的无知。我试图使用oauth2对twitter进行一个简单的RESTAPI调用,用于“仅应用程序”调用,但我一直将“无效或过期令牌”作为错误返回

有没有人有过这样的经验

响应为:{“错误”:[{“代码”:89,“消息”:“无效或过期的令牌。”}]}


结果证明我没有利用客户端凭证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)
}
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)
}