无法获得httpc基本身份验证示例以使用https

时间:2016-09-25 08:50:15

标签: erlang

这是我第一次使用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]。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

对于任何可能提供帮助的人 - 这正是我在原始问题中改变代码所做的工作 - 感谢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).