【问题标题】:erlang google oauth2 protocol for google calling apis用于谷歌调用 apis 的 erlang google oauth2 协议
【发布时间】:2023-03-04 10:05:01
【问题描述】:

您好,我正在编写 oauth 2 库来访问 google api,我的代码如下

    jwt_create() ->

    {ok,PemBin} = file:read_file("your-key-file.pem"),
    PemEntry = public_key:pem_decode(PemBin),
    [A,B] = PemEntry,
    io:format("A:: ~p ~n",[A]),
    PrivateKey = public_key:pem_entry_decode(PemEntry),
    JwtHeaderJson = encode_json(jwt_header()),
    JwtClaimsetJson = encode_json(jwt_claimset()),
    ComputeSignature = compute_signature(JwtHeaderJson, JwtClaimsetJson, PrivateKey),
    Z=binary:replace(
      binary:replace(<<JwtHeaderJson/binary, ".", JwtClaimsetJson/binary, ".", ComputeSignature/binary>>,
                     <<"+">>, <<"-">>, [global]),
      <<"/">>, <<"_">>, [global]),
    io:format("JWT:: ~p ~n",[Z]).
compute_signature(Header, ClaimSet,#'RSAPrivateKey'{publicExponent=Exponent

                                                          ,modulus=Modulus

                                                          ,privateExponent=PrivateExponent}) ->
    base64:encode(crypto:sign(rsa, sha256, <<Header/binary, ".", ClaimSet/binary>>, 
            [Exponent, Modulus, PrivateExponent])).
encode_json(JWToken) ->
    base64:encode(jsx:encode(JWToken)).

我收到如下错误:

异常错误:没有函数子句匹配 public_key:pem_entry_decode([{'PrivateKeyInfo',>, not_encrypted}, {'证书',>, not_encrypted}])(public_key.erl,第 123 行) 在函数 googleoauth:jwt_create/0 中(src/googleoauth.erl,第 55 行)

请帮助我为 OAUTH 2 生成 JWS 和 JWT 以访问 google api

【问题讨论】:

    标签: oauth-2.0 google-api erlang jwt erlangweb


    【解决方案1】:

    您将错误的东西传递给 public_key:pem_entry_decode/1:

    这将解决您的问题:

    PrivateKey = public_key:pem_entry_decode(A),
    

    public_key:pem_entry_decode/1 采用单个 pem_entry() 但 PEM 文件可以包含许多条目,也许您的代码 PemEntry = public_key:pem_decode(PemBin) 应该改为 PemEntries = public_key:pem_decode(PemBin)

    还要注意前面的行假设有 2 个列表条目,您可能是这个意思(虽然不确定您的意图)?

    [A|B] = PemEntry,
    

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多