# Failed to authenticate with Ledger API

**URL:** <https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498>\
**Category:** App Development\
**Tags:** extractor\
**Created:** [May 19, 2020, 5:23pm UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498 "2020-05-19T17:23:45Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 19, 2020, 5:23pm UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/1 "2020-05-19T17:23:45Z")

</div>

i am using extractor to extract all the contract data of a user but getting following error.

**Failed to authenticate with Ledger API on attempt 1, next one in 1 second**

\*\*

 ![upload](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/1X/f743a66b040891d3bef54edd89e2594cad17b980.png) \*\*

---

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [May 19, 2020, 6:49pm UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/2 "2020-05-19T18:49:31Z")

</div>

I’m not 100% sure, but I think you’ll need to provide an access token via the `--access-token-file` parameter. To get a default token that’s accepted by the sandbox you can use [https://jwt.io/](https://jwt.io/) . Enter the following payload (assuming ledger-id `test` here, change that to whatever you’re using):

```json
{
   "https://daml.com/ledger-api": {
     "ledgerId": "test",
     "participantId": null,
     "applicationId": null,
     "admin": true,
     "actAs": ["Validator"],
     "readAs": ["Validator"]
   },
   "exp": 1999999999
}

```

and use `secret` as the secret. You can use [this link](https://jwt.io/#debugger-io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJ0ZXN0IiwicGFydGljaXBhbnRJZCI6bnVsbCwiYXBwbGljYXRpb25JZCI6bnVsbCwiYWRtaW4iOnRydWUsImFjdEFzIjpbIlZhbGlkYXRvciJdLCJyZWFkQXMiOlsiVmFsaWRhdG9yIl19LCJleHAiOjE5OTk5OTk5OTl9.ARSLC2BdbRpM1goMyae_vpbyVf6e4CH3q1epfJKrTPQ) and adjust the payload to your needs.

Put that into a file, eg `token.txt` and run the extractor with `--access-token-file=token.txt`. Can you try that and see if it works?

---

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 20, 2020, 4:06am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/3 "2020-05-20T04:06:48Z")

</div>

Even after using token file, I am getting the same error and also do I have to create table as specified in [docs] ([https://docs.daml.com/tools/extractor.html#transactions](https://docs.daml.com/tools/extractor.html#transactions)) of Transaction, Contract etc or it automatically create while extracting.

---

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [May 20, 2020, 6:43am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/4 "2020-05-20T06:43:48Z")

</div>

It could be that you have to prefix the token in the file with `Bearer ` so your `token.txt` looks like this:

```haskell
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJ0ZXN0IiwicGFydGljaXBhbnRJZCI6bnVsbCwiYXBwbGljYXRpb25JZCI6bnVsbCwiYWRtaW4iOnRydWUsImFjdEFzIjpbIlZhbGlkYXRvciJdLCJyZWFkQXMiOlsiVmFsaWRhdG9yIl19LCJleHAiOjE5OTk5OTk5OTl9.ARSLC2BdbRpM1goMyae_vpbyVf6e4CH3q1epfJKrTPQ

```

If that doesn’t work, can you paste the content of your `token.txt` (assuming it’s for testing only) here so I can validate it?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 20, 2020, 6:58am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/5 "2020-05-20T06:58:48Z")

</div>

Extractor does not require a dummy token like the JSON API. If it complains about needing a token it is because your ledger is actually running with authentication and it will validate your token. In that case, you can only use `secret` as the secret if you started sandbox with `--auth-jwt-hs256-unsafe=secret`. If you used another secret you will have to change it and if you used one of the `--auth-jwt-es256-crt`-like flags you will have to use the correspond algorithm when creating your token.

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 7:20am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/6 "2020-05-20T07:20:18Z")

</div>

> [@georg](#):
>
> It could be that you have to prefix the token in the file with `Bearer ` so your `token.txt` looks like this:
> 
> ```haskell
> Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJ0ZXN0IiwicGFydGljaXBhbnRJZCI6bnVsbCwiYXBwbGljYXRpb25JZCI6bnVsbCwiYWRtaW4iOnRydWUsImFjdEFzIjpbIlZhbGlkYXRvciJdLCJyZWFkQXMiOlsiVmFsaWRhdG9yIl19LCJleHAiOjE5OTk5OTk5OTl9.ARSLC2BdbRpM1goMyae_vpbyVf6e4CH3q1epfJKrTPQ
> 
> ```
> 
> If that doesn’t work, can you paste the content of your `token.txt` (assuming it’s for testing only) here so I can validate it?

Note that this is true only for versions before 1.0.0. The schema has been set to `Bearer` and it doesn’t have to be prefixed in token files. [This](https://github.com/digital-asset/daml/pull/5484) is the PR that introduced the change for a few components of the SDK, including Extractor.

---

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 20, 2020, 8:34am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/7 "2020-05-20T08:34:26Z")

</div>

i didn’t use any auth-jwt while starting sandbox as you can see in image

 ![second](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/1X/45f5e5481ceee3880e60d12c03b33eafdf1b9203.png)

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 8:59am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/8 "2020-05-20T08:59:38Z")

</div>

I have had a better look at the output of the first screenshot you shared (hint: you can use code snippets to make shared logs easier to read 😉 ) and it looks like extractor has shown a spurious error but completed its task successfully. Can you have a look at the database and confirm, please?

---

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 20, 2020, 9:01am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/9 "2020-05-20T09:01:52Z")

</div>

I looked into database tables but each one is empty only schema is created.

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 9:04am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/10 "2020-05-20T09:04:48Z")

</div>

And if you have a look at Navigator using `Validator` as a party, do you see active or archived contracts?

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 9:10am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/11 "2020-05-20T09:10:18Z")

</div>

> [@stefanobaghino-da](#):
>
> I have had a better look at the output of the first screenshot you shared (hint: you can use code snippets to make shared logs easier to read 😉 ) and it looks like extractor has shown a spurious error but completed its task successfully. Can you have a look at the database and confirm, please?

tl;dr the error you see is spurious but not fatal, if you don’t see the expected data on Extractor the cause is something else.

As further information: that message definitely looks wrong and we should look into why that’s happening, but please note that Extractor will keep retrying to connect to a Ledger API server protected by authentication indefinitely, so if you only see that message once it _should_ be something you’re aware of but not a fatal error. You can expect that to happen if you are working against a Ledger API server protected by authentication and the key rotation is delayed or doesn’t work.

---

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 20, 2020, 9:19am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/12 "2020-05-20T09:19:40Z")

</div>

Yes, Extractor is continuously trying to connect and giving following error each time.

```
14:40:49.400 INFO c.d.a.TokenHolder@[kka.actor.default-dispatcher-6] - Reading token from token.txt...
14:40:49.401 INFO c.d.e.Extractor@[kka.actor.default-dispatcher-6] - Starting streaming transactions from Absolute(000000000000000f0000000000000000)...
14:40:49.424 WARN a.s.s.RestartWithBackoffSource@[kka.actor.default-dispatcher-7] - Restarting graph due to failure. stack_trace: 
io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Invalid argument: Begin offset 000000000000000f0000000000000000 is after ledger end 000000000000000a0000000000000000
	at io.grpc.Status.asRuntimeException(Status.java:533)
	at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:442)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:700)
	at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
	at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
	at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:399)
	at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:507)
	at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:66)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:627)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$700(ClientCallImpl.java:515)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:686)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:675)
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
```

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 9:24am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/13 "2020-05-20T09:24:42Z")

</div>

Ok, a couple of notes: if you are not running `daml start` specifying that you want to run in authenticated mode, do not pass in a token file (the log shows that you are passing `token.txt`).

Regarding the error you see, I believe that might be a bug, I’ll check it. In the meantime, would you mind checking if you still see the error if you run `daml start --sandbox-classic`?

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [May 20, 2020, 9:33am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/14 "2020-05-20T09:33:13Z")

</div>

I’ll need to have more details regarding your setup because the error you are seeing is very weird and it seems to point at several restarts using the same persistent backend using different SDK versions.

1. what SDK version are you using?
2. are you using sandbox or sandbox-classic? `daml start` and `daml sandbox` run sandbox-classic by default up until version 0.13.55 included and sandbox starting from version 1.0.0
3. are you using some form of persistent backend (i.e. `--sql-backend-jdbcurl`) when you run `daml start` or `daml sandbox`?
4. if yes, have you been using the same persistent backend across SDK upgrades?

Please do add as many details as possible and don’t stop at providing the details I asked explicitly, the more details the easier it’ll be for me to investigate and the quicker you’ll have an answer. 😉

---

<div class="post-metadata">

**Author:** ![Nishchal\_Vashisht](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/nishchal_vashisht/32/362_2.png) [@Nishchal\_Vashisht](https://forum.canton.network/u/Nishchal_Vashisht)\
**Post date:** [May 20, 2020, 10:43am UTC](https://forum.canton.network/t/failed-to-authenticate-with-ledger-api/498/15 "2020-05-20T10:43:11Z")

</div>

Thanks @stefanobaghino-da sorry I was little bit confused by the authentication error but after removing token file I am still seeing the  
**15:56:35.106 ERROR c.d.e.Extractor@[ka.actor.default-dispatcher-12] - Failed to authenticate with Ledger API on attempt 1, next one in 1 second**

but finally, all party contract detail is extracted from the ledger and stored in Postgres table. Thanks again
