Twitter oauth golang获取错误"代码":32,"消息":"无法验证您。"

时间:2016-12-23 05:08:41

标签: twitter go twitter-oauth

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

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

  

{"错误":[{"代码":32,"消息":"无法对您进行身份验证。"}] }

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
}

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

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
}

1 个答案:

答案 0 :(得分:0)

能够解决问题。我做了两处修改:

  1. 在传递加密以创建签名

    之前对params变量进行编码

    baseString:=" POST" +"&" + encode(twitterConf.Endpoint.RequestTokenUrl)+"&" + encode(params。编码())

  2. 要创建标题,我创建了自己的标题字符串

        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)

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