Question: Got the credentials for Service Account from Developer Console

First, I converted p12 private key to PEM:

```
openssl pkcs12 -in <private key for Service Account>.p12 -out calendar.key -nocerts -nodes

```

Then I run:

```
use MIME::Base64;
use Crypt::OpenSSL::RSA;
use File::Slurp;

my $header = encode_base64('{"alg":"RS256","typ":"JWT"}','');
my $claim = encode_base64('{
"iss":"<mail for the Service Account>",
"scope":"https://www.googleapis.com/auth/calendar",
"aud":"https://accounts.google.com/o/oauth2/token",
"exp":'.(time()+3600).',
"iat":'.time().'
}','');

my $key = read_file('calendar.key');
my $rsa = Crypt::OpenSSL::RSA->new_private_key($key);
$rsa->use_sha256_hash;
$rsa->use_pkcs1_padding;
my $signature = encode_base64($rsa->sign($header . '.' . $claim), '');

my $token_request = $header . '.' . $claim . '.' . $signature;

print `curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=$token_request' https://accounts.google.com/o/oauth2/token`;

```

I get 

```
{
  "error" : "invalid_grant"
}

```

I synchronized the system time with NTP, didn't help.

Answer: 
I don't know why, but the problem was with `curl`.

I replaced it by [`WWW::Mechanize`](https://metacpan.org/pod/WWW%3a%3aMechanize):

```
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->post('https://accounts.google.com/o/oauth2/token',
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Content' => [
        'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion' => $token_request,
    ],
);

```

and it works.
Change your curl command to be:

```
curl -H "Content-Type: application/x-www-form-urlencoded" \
     -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
     -d assertion=$token_request \
      https://accounts.google.com/oauth2/token

```