【问题标题】:Can't get httpc basic auth example to work with https无法让 httpc 基本身份验证示例与 https 一起使用
【发布时间】:2016-09-26 17:44:29
【问题描述】:

这是我第一次使用 erlang,我决定尝试为 API 编写一个包装器。这是我到目前为止所得到的:-

            -module(my_api_wrapper).

            %% API exports
            -export([auth/0]).

            auth() ->
                application:start(inets),
                application:start(ssl),
                AuthStr = base64:encode_to_string("username:password"),

                Method = post,
                URL = "https://api.endpoint.com/auth",
                Header = [{"Authorization", "Basic " ++ AuthStr}],
                Type = "application/json",
                Body = "{\"grant_type\":\"client_credentials\"}",
                HTTPOptions = [],
                Options = [],
                httpc:request(Method, {URL, Header, Type, Body}, HTTPOptions, Options).

在 shell 上进行测试时出现错误:-

{error,{failed_connect,[{to_address,{"api.endpoint.com",
                                 443}},
                    {inet,[inet],closed}]}}

我无法弄清楚我在这里做错了什么!我正在运行这个版本的 Erlang/OTP 19 [erts-8.0.2]。任何帮助表示赞赏。

【问题讨论】:

  • 这个问题的答案有帮助吗:stackoverflow.com/questions/38620111/…?
  • 是的!这帮助我修复了它。感谢您的帮助!
  • 一般提示:我建议使用{ok, _} = application:ensure_all_started(ssl) -- 要求 OTP 处理应用程序依赖关系并确保应用程序实际启动。

标签: erlang


【解决方案1】:

对于任何可能有帮助的人 - 这正是我为使原始问题中的代码正常工作所做的更改 - 感谢 Dogbert 在上面的评论。

        -module(my_api_wrapper).

        %% API exports
        -export([auth/0]).

        auth() ->
            application:start(inets),
            application:start(ssl),
            AuthStr = base64:encode_to_string("username:password"),

            Method = post,
            URL = "https://api.endpoint.com/auth",
            Header = [{"Authorization", "Basic " ++ AuthStr}],
            Type = "application/json",
            Body = "{\"grant_type\":\"client_credentials\"}",
            % ADD SSL CONFIG BELOW!
            HTTPOptions = [{ssl,[{versions, ['tlsv1.2']}]}], 
            Options = [],
            httpc:request(Method, {URL, Header, Type, Body}, HTTPOptions, Options).

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2013-03-17
    • 2018-11-10
    • 2014-06-06
    相关资源
    最近更新 更多