[英]erlang google oauth2 protocol for google calling apis
您好,我正在編寫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',<< 48,130,4,191,2,1,0,48,13,6,9,42,134,72,134,247,13,1,1,1,5,5 ,0,4,130,4,... >>,not_encrypted},{'證書',<< 48,130,3,96,48,130,2,72,160,3,2,1,2,2,8,79,59,244 ,35,60,15,3,155,48,... >>,not_encrypted}])(函數googleoauth:jwt_create / 0(src / googleoauth.erl,第55行)中的public_key.erl,第123行)
請幫助我為OAUTH 2生成JWS和JWT以訪問Google API
您將錯誤的信息傳遞給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,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.