# Google Service Account signing JWT

**URL:** <https://community.postman.com/t/google-service-account-signing-jwt/9675>\
**Category:** Help Hub\
**Created:** [December 17, 2019, 11:29pm UTC](https://community.postman.com/t/google-service-account-signing-jwt/9675 "2019-12-17T23:29:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![postman-kyle](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@postman-kyle](https://community.postman.com/u/postman-kyle)\
**Post date:** [December 17, 2019, 11:29pm UTC](https://community.postman.com/t/google-service-account-signing-jwt/9675/1 "2019-12-17T23:29:17Z")

</div>

I am trying to get an access token using a request for Google Service Account.

I am trying to create a JWT to get an access token for a Google Service Account to use in a Postman Pre-Request script.

But I keep getting this error in the Postman Console:

```auto
Error:

```

And in the Postman Response window I get:

```auto
There was an error in evaluating the Pre-request Script: undefined: undefined

```

I localized the error to be failing here:

```auto
var jwt = KJUR.jws.JWS.sign(null, header, claimSet, privateKey);

```

I’m importing it by using a request to `http://kjur.github.io/jsrsasign/jsrsasign-latest-all-min.js` and saving it to an environment variable called `jsrsasign` then rehydrating it below with eval.

Pre-Request Script:

```auto
var navigator = {};
var window = {};
eval(pm.environment.get("jsrsasign"));

var scope = pm.environment.get('scope');
var iss = pm.environment.get('iss');
var privateKey = pm.environment.get('privateKey');

const header = {"alg" : "RS256", "typ" : "JWT"};

const claimSet =
{
  "iss": iss,
  "scope": scope,
  "aud":"https://oauth2.googleapis.com/token",
  "exp":KJUR.jws.IntDate.get("now + 1hour").toString(),
  "iat": KJUR.jws.IntDate.get("now").toString()
}

console.log(`header: ${ JSON.stringify(header)}`);
console.log(`claim set: ${ JSON.stringify(claimSet) }`);

var jwt = KJUR.jws.JWS.sign(null, header, claimSet, privateKey);

pm.environment.set('jwt', jwt);

```

Similar issue to this guy’s comment on Medium:

> **[Hi, Klaasjan Tukker I’m trying to get the library import and eval to work for...](https://medium.com/@jkohne/hi-klaasjan-tukker-im-trying-to-get-the-library-import-and-eval-to-work-for-jsrsasign-c0c457ddd23f)**
>
> I also tried removing the navigator and window faking statements, because Postman has them built-in, but that resulted in the module…

Here is my StackOverflow post if you guys are interested in gaining some reputation as well:

> <https://stackoverflow.com/questions/59383521/postman-pre-request-script-to-create-jwt-for-google-service-account>

---

<div class="post-metadata">

**Author:** ![postman-kyle](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@postman-kyle](https://community.postman.com/u/postman-kyle)\
**Post date:** [December 18, 2019, 12:41am UTC](https://community.postman.com/t/google-service-account-signing-jwt/9675/2 "2019-12-18T00:41:26Z")

</div>

Seems to be an issue when I try and call the JSRSASign library it doesn’t have all the methods I expected. In fact all I see in it’s JWS object is:

```
 {parseJWS: "[Function]"}

```

---

<div class="post-metadata">

**Author:** ![postman-kyle](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@postman-kyle](https://community.postman.com/u/postman-kyle)\
**Post date:** [December 19, 2019, 4:57pm UTC](https://community.postman.com/t/google-service-account-signing-jwt/9675/3 "2019-12-19T16:57:07Z")

</div>

Solved this:

My private key had escaped white space characters in it ( `\n` , `\t` )

I opened up google chrome dev tools and just saved it to a variable with template literals and console logged it out to get a properly formatted key.

Another option may just to do that in Postman as well.

After that, it worked.

Published Collection: [Google Service Account Sheets Demo](https://documenter.getpostman.com/view/8140651/SWECYFyf?version=latest)
