【发布时间】: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