title: How can I get JWT with Ruby?
body: <p>I am currently working on the project that I am going to integrate the application of my company and salesforce.</p>
  <p>In my case, it seemed that using the JWT for authentication is better.
  So, I wanted to try it.</p>
  <p>but I don't know how to generate JWT on Ruby though I researched a lot.
  So if anyone knows how to make jwt, I would love you to tell me how to do it.</p>
  <p>What I wanted to do is that
  1, create application on salesforce (done)
  2, create X509 certification and set it on the application on salesforce. (done)
  3, create JWT by using the secret key of X509 certification. (don't know how)</p>
  <p>I have been looking for the way to solve this but I couldn't.
  I guess I should be doing something like this.</p>
  <pre><code>require 'jwt'
  
  payload = { data: 'test' }
  
  # The secret must be a string. A JWT::DecodeError will be raised if it isn't provided.
  hmac_secret = 'my$ecretK3y'
  
  token = JWT.encode payload, hmac_secret, 'HS256'
  
  # eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pNIWIL34Jo13LViZAJACzK6Yf0qnvT_BuwOxiMCPE-Y
  puts token
  
  decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
  
  # Array
  # [
  #   {&quot;data&quot;=&gt;&quot;test&quot;}, # payload
  #   {&quot;alg&quot;=&gt;&quot;HS256&quot;} # header
  # ]
  puts decoded_token
  </code></pre>
  <p>but this doesn't work on my local.
  When I run this file, it says</p>
  <pre><code>`require': cannot load such file -- jwt
  </code></pre>
  <p>I think PHP version of what I want to do is this.</p>
  <pre><code>    &lt;?php
  
  require_once './vendor/autoload.php';
  
  use Lcobucci\JWT\Builder;
  use Lcobucci\JWT\Signer\Key;
  use Lcobucci\JWT\Signer\Rsa\Sha256;
  
  // ログインURL
  // 本番: https://login.salesforce.com
  // Sandbox: https://test.login.salesforce.com
  // スクラッチ組織: https://test.saleforce.com
  define('LOGIN_URL', 'https://test.salesforce.com');
  // コンシューマ鍵
  define('CLIENT_ID', &lt;&lt;接続アプリケーションのコンシューマ鍵&gt;&gt;);
  // ユーザID
  define('USER_ID', 'xxxxx@example.com');
  
  function createjwt() {
  
     $signer = new Sha256();
     $privateKey = new Key('file://cert/server.key');
     $time = time();
     
     $token = (new Builder())-&gt;issuedBy(CLIENT_ID) // iss: コンシューマ鍵
                             -&gt;permittedFor(LOGIN_URL) // aud: SalesforceログインURL
                             -&gt;relatedTo(USER_ID) // sub: SalesforceユーザID
                             -&gt;expiresAt($time + 3 * 60) // exp: 3分以内
                             -&gt;getToken($signer,  $privateKey);
  
     return $token;
  }
  
  $jwt = createjwt();
  
  echo $jwt;
  </code></pre>
  <p>If you have any clue please help me.</p>
  <p>references are here:
  <a href="https://help.salesforce.com/articleView?id=sf.remoteaccess_oauth_jwt_flow.htm&amp;type=5" rel="nofollow noreferrer">https://help.salesforce.com/articleView?id=sf.remoteaccess_oauth_jwt_flow.htm&amp;type=5</a>
  <a href="https://github.com/jwt/ruby-jwt" rel="nofollow noreferrer">https://github.com/jwt/ruby-jwt</a></p>