Drone

401 Unauthorized on /api/repos?async=true before ui log in

Hi,

I am trying to sync repositories for a user created via the DRONE_USER_CREATE=username:[-gogs_username-],machine:false,admin:true,token:[-drone_token-] env var inside docker.

The user is created fine and I can verify this by making a get request Authorized with the token to /api/user.

For some reason the repositories refuse to sync until after I have manually logged in on the dashboard.

I tried sending a post with the login credentials to the login endpoint but that did not trigger an update for the login field on the user object returned.

I’m 100% sure I’m using correct authentication as it works on all other endpoints, tested in-code and via postman/browser.

Happy to send more info if required.

For some reason the repositories refuse to sync until after I have manually logged in on the dashboard.

This is expected. The user account cannot interact with GitHub until you have gone through the oauth2 login process, which generates an oauth2 access token and refresh token, and authorizes Drone to access data on your behalf.

As you can tell by my env var I’m actually trying to run this with Gogs. The endpoint I’m trying to call is /api/user/repos?async=true and the debug simply says 401 unauthorised.

Is there any other endpoint I can hit to make the initial sync happen?

Or any other workaround?

Under the hood this endpoint makes an API call to github/gitlab/gogs/gitea/bitbucket and requires an access token. This token is only available after a login. It is therefore required to login before you can invoke this endpoint.

Is there any other way to simulate the login/ or generate the access token? My use case:

  • new server provisioned
  • user generated on gogs and then drone
  • gogs repo made
  • repos are synced in drone
  • push to gogs repo
  • we hook triggers build

I basically want to be able to push and deploy to that new server without having to touch it at all. This is the last piece in the puzzle for me.