本文介绍了Twitter oauth golang 收到错误“代码":32,“消息":“无法对您进行身份验证".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 golang 中实现使用 Twitter 帐户登录.我是我尝试获取请求令牌的第一步.作为参考,我使用了以下链接中提供的 mrjones 代码.https://github.com/mrjones/oauth/blob/master/examples/twitterserver/twitterserver.go

I am trying to do implement signing in with twitter account in golang. I am the first step where in I am trying to get the request token. As a reference I used the mrjones's code available at below link. https://github.com/mrjones/oauth/blob/master/examples/twitterserver/twitterserver.go

我收到以下错误.请让我知道我哪里出错了:

I am getting following error. Please let me know where am I going wrong:

{"errors":[{"code":32,"message":"无法验证您的身份."}]}

var twitterConf = &TwitterConfig{
    ClientID:     " my consumer key",
    ClientSecret: "my consumer key secret",
    RedirectURL:  "http://localhost:8080/oauth/twitterOauth2callback",

    Endpoint: ServiceProvider{
        RequestTokenUrl:   "https://api.twitter.com/oauth/request_token",
        AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
        AccessTokenUrl:    "https://api.twitter.com/oauth2/token",
    },
}

func HandletwitterLogin(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {

    ctx := appengine.NewContext(req)

    params := url.Values{}
    params.Add(CALLBACK_PARAM, twitterConf.RedirectURL)
    params.Add(CONSUMER_KEY_PARAM, twitterConf.ClientID)
    params.Add(NONCE_PARAM, strconv.FormatInt(rand.New(rand.NewSource(time.Now().UnixNano())).Int63(), 10))
    params.Add(SIGNATURE_METHOD_PARAM, SIGNATURE_METHOD_HMAC+"SHA1")
    params.Add(TIMESTAMP_PARAM, strconv.FormatInt(time.Now().Unix(), 10))
    params.Add(VERSION_PARAM, OAUTH_VERSION)
    params.Add("oauth_token", "my oauth token")

    baseString := requestString("POST", twitterConf.Endpoint.RequestTokenUrl, params)

    signature, err6 := Sign(baseString, "my token secret")

    params.Add(SIGNATURE_PARAM, signature)

    Url, err := url.Parse(twitterConf.Endpoint.RequestTokenUrl)

    Url.RawQuery = params.Encode()
    firsturl := Url.String()

    reqnew, err2 := http.NewRequest("POST", firsturl, nil)
    if err2 != nil {
        log.Errorf(ctx, "ERROR IN CREATING NEW REQUEST %+v ", err2)
    }

    reqnew.Header.Add("Authorization", "OAuth ")

    client := urlfetch.Client(ctx)

    resp, err3 := client.Do(reqnew)
    if err3 != nil {
        log.Errorf(ctx, "ERROR IN doing the client request  %+v ", err3)
    }

    bodyBytes, err4 := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    log.Infof(ctx, "HandletwitterLogin 5 ")
    if err4 != nil {
        log.Errorf(ctx, "ERROR IN READALL RESP BODY   %+v ", err4)
    }

    bodyStr := string(bodyBytes)

//Here i am getting above mentioned error
}

这些是我使用的其他功能.

These are the other functions which I used.

func requestString(method string, url string, params url.Values) string {
    result := method + "&" + escape(url)

    for key, value := range params {
        if len(value) > 0 {
            result += escape("&")

            result += escape(fmt.Sprintf("%s=%s", key, value))
        }
    }
    return result
}

func Sign(message string, tokenSecret string) (string, error) {
    key := escape(twitterConf.ClientSecret) + "&" + escape(tokenSecret)

    h := hmac.New(crypto.SHA1.New, []byte(key))
    h.Write([]byte(message))
    rawSignature := h.Sum(nil)

    base64signature := base64.StdEncoding.EncodeToString(rawSignature)
    return base64signature, nil
}

推荐答案

能够解决问题.我做了两个更改:

Able to solve the problem. I made two changes:

  1. 在传递用于加密以创建签名之前对 params 变量进行编码

  1. encoded the params variables before passing for encryption to create signature

baseString := "POST"+"&"+ encode(twitterConf.Endpoint.RequestTokenUrl) + "&"+encode(params.Encode())

baseString := "POST"+"&"+ encode(twitterConf.Endpoint.RequestTokenUrl) + "&"+encode(params.Encode())

为了创建标题,我创建了自己的标题字符串

To create header, I created my own header string

    headerstring := "OAuth oauth_callback=\"http%3A%2F%2Flocalhost%3A8080%2Foauth%2FtwitterOauth2callback\",oauth_consumer_key=\"consumerkey\",oauth_nonce=\"" +
        params.Get(NONCE_PARAM) + "\",oauth_signature=\"" + escape(signature) + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + params.Get(TIMESTAMP_PARAM) +
        "\",oauth_token=\"" + escape("oauth token") + "\",oauth_version=\"1.0\""

reqnew.Header.Add("Authorization", headerstring)

reqnew.Header.Add("Authorization", headerstring)

这解决了问题,我开始获取访问令牌

This solved the problem and I started getting access token

这篇关于Twitter oauth golang 收到错误“代码":32,“消息":“无法对您进行身份验证".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:00