首页 文章

使用cURL和用户名和密码?

提问于
浏览
347

我想访问需要用户名/密码的URL . 我想尝试用curl访问它 . 现在我做的事情如下:

curl http://api.somesite.com/test/blah?something=123

我收到一个错误 . 我想我需要指定一个用户名和密码以及上面的命令 .

我怎样才能做到这一点?

13 回答

  • 540

    使用 -u 标志包含用户名,curl将提示输入密码:

    curl -u username http://example.com
    

    您也可以在命令中包含密码,但是密码将在bash历史记录中显示:

    curl -u username:password http://example.com
    
  • 2

    这样做更安全:

    curl --netrc-file my-password-file http://example.com
    

    ...因为在命令行上传递普通用户/密码字符串是个坏主意 .

    密码文件的格式是(根据 man curl ):

    machine <example.com> login <username> password <password>
    

    (单词' machine ', ' login ', and ' password '只是关键字;实际信息是这些关键字之后的内容) .

  • 59

    或者同样的事情,但不同的语法

    curl http://username:password@api.somesite.com/test/blah?something=123
    
  • 2

    您也可以通过写入来发送用户名:

    curl -u USERNAME http://server.example
    

    然后,Curl将询问您密码,并且屏幕上将不会显示密码(或者如果您需要复制/粘贴命令) .

  • 7

    要在脚本中安全地传递密码(即防止它显示ps auxf或日志),您可以使用-K-标志(从stdin读取配置)和heredoc:

    curl --url url -K- <<< "--user user:password"
    
  • 1
    curl -X GET -u username:password  {{ http://www.example.com/filename.txt }} -O
    
  • 1

    要让密码至少不在 .bash_history 中弹出:

    curl -u user:$(cat .password-file) http://example-domain.tld
    
  • 175

    简单而简单地说,最安全的方法是使用环境变量来存储/检索您的凭据 . 因此卷曲命令如:

    curl -Lk -XGET -u "${API_USER}:${API_HASH}" -b cookies.txt -c cookies.txt -- "http://api.somesite.com/test/blah?something=123"
    

    然后调用您的restful api并传递带有_6441216_和 API_HASH 的Base64编码值的http WWW_Authentication 标头 . -Lk 只是告诉curl遵循http 30x重定向并使用不安全的tls处理(即忽略ssl错误) . 虽然双 -- 只是bash语法糖来停止处理命令行标志 . 此外, -b cookies.txt-c cookies.txt 标志处理带有 -b 发送cookie的cookie和 -c 在本地存储cookie .

    该手册有更多examples of authentication方法 .

  • 13

    如果您使用的是具有Gnome密钥环应用程序的系统,则避免直接暴露密码的解决方案是使用gkeyring.py从密钥环中提取密码:

    server=server.example.com
    file=path/to/my/file
    user=my_user_name
    pass=$(gkeyring.py -k login -tnetwork -p user=$user,server=$server -1)
    
    curl -u $user:$pass ftps://$server/$file -O
    
  • 66

    您可以使用命令,如,

    curl -u user-name -p http://www.example.com/path-to-file/file-name.ext > new-file-name.ext
    

    然后将触发HTTP密码 .

    参考:http://www.asempt.com/article/how-use-curl-http-password-protected-site

  • 11

    我在bash(Ubuntu 16.04 LTS)中有同样的需求,答案中提供的命令无法在我的情况下工作 . 我不得不使用:

    curl -X POST -F 'username="$USER"' -F 'password="$PASS"' "http://api.somesite.com/test/blah?something=123"
    

    只有在使用变量时才需要 -F 参数中的双引号,因此从命令行 ... -F 'username=myuser' ... 就可以了 .

    如果评论或编辑可以解释原因,我会很高兴!

  • 8

    其他答案建议netrc根据我读过的内容指定用户名和密码,我同意 . 以下是一些语法细节:

    https://ec.haxx.se/usingcurl-netrc.html

    与其他答案一样,我想强调需要注意这个问题的安全性 .

    虽然我不是专家,但我发现这些链接很有见地:

    https://ec.haxx.se/cmdline-passwords.html

    总结一下:

    使用 encrypted versions of the protocols (HTTPS与HTTP)(FTPS与FTP)可以帮助避免网络泄漏 .

    使用 netrc 可以帮助避免命令行泄漏 .

    为了更进一步,似乎你也可以使用gpg加密netrc文件

    https://brandur.org/fragments/gpg-curl

    有了这个,您的凭据不会以“纯文本”“静止”(存储) .

  • 1

    很容易,请执行以下操作:

    curl -X GET/POST/PUT <URL> -u username:password
    

相关问题