Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 9

Random 401 errors after using freshly generated installation access token

Hey!

 

I am getting random "401 — Bad credentials" errors while using freshly generated installation token for Github App installation. It works 99% of the time but from time to time it fails.

 

What I do exactly:

 

CALL -> /app/installations/X/access_tokens
RESPONSE <- {"token":"XXX","expires_at":"2019-04-30T08:07:39Z"}

 

everything looks good so far, note: expires_at is a future date

 

I use received token to call API like repos/X/Y/pulls/2823/files and from time to time I get 401 error exactly like GitHub API was not internally consistent. 

 

Any pointers how should I solve this? I thought about simply retrying it before really failing.

8 Replies
Community Manager
Message 2 of 9

Re: Random 401 errors after using freshly generated installation access token

We're running a few different Probot framework GitHub Apps and haven't seen this kind of intermittent failure. Are you using a framework like Probot or Octokit to interact with the GitHub API?

Copilot Lvl 3
Message 3 of 9

Re: Random 401 errors after using freshly generated installation access token

I just make raw http request — no library. I noticed that octokit allows to easily implement retry strategy. Probably I do just this and retry 401 with some delay. 

 

@lee-dohm do you cache installation access tokens in any way on your end? I noticed that they are valid for ~one hour so they could be easily reused between requests which probably could also make this problem go away (at least till some extent). 

Copilot Lvl 3
Message 4 of 9

Re: Random 401 errors after using freshly generated installation access token

Happening for us at GitSpeak well. Very annoying.

Community Manager
Message 5 of 9

Re: Random 401 errors after using freshly generated installation access token

I'd have to go digging into the Probot internals to see how it caches installation tokens. If I get a chance to do so, I'll see if I can find it. Or you could take a look and duplicate the code ... or convert to using Probot 😀 That's why we built the Probot framework, to make all of this easier.

Community Manager
Message 6 of 9

Re: Random 401 errors after using freshly generated installation access token

Copilot Lvl 2
Message 7 of 9

Re: Random 401 errors after using freshly generated installation access token

We're also seeing this random 401 errors when using github apps. Every now and then it happens when retrieving installation tokes, but more commonly it happens after getting an installation token and using it for other API calls. Same call might work 5 times and then get a 401 the next one, or get 401 as soon as we make the first call

Community Manager
Message 8 of 9

Re: Random 401 errors after using freshly generated installation access token

I've talked to the developers and the current advice is the following:

 

  • Cache access tokens and reuse them until they expire (currently 60 minutes)
  • Avoid making many simultaneous requests immediately after generating a new access token
  • Retry failed operations with exponential backoff up to a reasonable maximum number

 

I hope that helps and let us know what you find after implementing 👆

Copilot Lvl 3
Message 9 of 9

Re: Random 401 errors after using freshly generated installation access token

Thanks! I am gonna implement those ideas and let you know if it really solved our problems.