linux - 为什么这个 twurl 命令(在 linux 上运行,由 golang exec 运行)没有经过身份验证?
问题描述
我目前正在开发一个将更新 Twitter 状态的 Golang 网站(在 Ubuntu 上运行)。我在系统上使用了 twurl 客户密钥认证,如果我直接在 linux 终端中输入,我可以成功更新状态。例如
- ssh/putty 进入目标系统
- 在终端输入: twurl -d 'status=is this thing on' /1.1/statuses/update.json
- 推特状态已成功更新
当我尝试通过 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."}。我怀疑它与命令的设置有关......???或者也许是状态参数中单引号的解释???
解决方案
不要包含单引号:
statusarg := "status=" + status
在 Golang 中,"twurl"
是一个包含twurl
. 特别是,字符串数据不包括双引号。这些引号只是字符串的 Go 语法。
在 Bash 中,'status=Foo'
是一个包含status=Foo
. 特别是,字符串数据不包括单引号。这些只是未插值字符串的 Bash 语法。
推荐阅读
- eclipse - 如何使用 junit 5 为 Eclipse 编写测试套件?
- wordpress - 了解服务器日志文件
- python - 在 Django(drf 和 simplejwt)中究竟应该如何实现基于 JWT 的身份验证?
- javascript - 2.0 === 2 的 javascript 相等性 - 适用于所有浏览器
- node.js - Nuxtjs 如何使用 Nodejs 在 openlitespeed 上启动?启动文件示例
- bixby - 错误 此版本的 viv.geo 包含错误,bixby 需要升级
- lisp - 如果条件与运算符
- r - R 中 rlang::enexpr() 中的行为 as.list
- django - Django:芹菜为同一任务显示不同的状态
- python - 字典,从键调用值