首页 > 解决方案 > 为什么这个 twurl 命令(在 linux 上运行,由 golang exec 运行)没有经过身份验证?

问题描述

我目前正在开发一个将更新 Twitter 状态的 Golang 网站(在 Ubuntu 上运行)。我在系统上使用了 twurl 客户密钥认证,如果我直接在 linux 终端中输入,我可以成功更新状态。例如

  1. ssh/putty 进入目标系统
  2. 在终端输入: twurl -d 'status=is this thing on' /1.1/statuses/update.json
  3. 推特状态已成功更新

当我尝试通过 Golang exec 执行相同操作时,twitter 给了我一个身份验证错误。

func UpdateMainStatus(status string) {
    statusarg := `'` + "status=" + status + `'`
    out, err := exec.Command("twurl", "-d", statusarg, "/1.1/statuses/update.json").Output()
}

我尝试了几种不同的方法来格式化 statusarg。将上述状态参数打印到控制台显示:'status=Windows 工作,在 ubuntu 上测试更新。'

Twurl 似乎确实运行了,因为 out 显示来自 twitter 的错误响应,{"code":32,"message":"Could not authenticate you."}。我怀疑它与命令的设置有关......???或者也许是状态参数中单引号的解释???

标签: linuxgoexectwurl

解决方案


不要包含单引号:

statusarg := "status=" + status

在 Golang 中,"twurl"是一个包含twurl. 特别是,字符串数据不包括双引号。这些引号只是字符串的 Go 语法。

在 Bash 中,'status=Foo'是一个包含status=Foo. 特别是,字符串数据不包括单引号。这些只是未插值字符串的 Bash 语法。


推荐阅读