adriangodong / githubjwt

A helper library to generate GitHub JWT from GitHub Apps private key
MIT License
14 stars 6 forks source link

Bad credentials #5

Closed github-apptesting closed 3 years ago

github-apptesting commented 6 years ago

Hi, I used the DLLs as per documentation but I was consistently getting error saying bad credentials. Thanks

github-apptesting commented 6 years ago

This is how my code looks like -

StringPrivateKeySource privateKeySource = new StringPrivateKeySource(
                --key initialization code--);
            GitHubJwtFactoryOptions options = new GitHubJwtFactoryOptions
            {
                AppIntegrationId = --Id initialization code--),
                ExpirationSeconds = MaxTokenExpirationSeconds
            };

            GitHubJwtFactory factory = new GitHubJwtFactory(privateKeySource, options);
            string token = factory.CreateEncodedJwtToken();
            m_gitHttpClient = new GitHubClient(
                new ProductHeaderValue(
                    --header initialization code--));
github-apptesting commented 6 years ago

This is the exception -

INNER EXCEPTION, System.AggregateException: One or more errors occurred. ---> Octokit.AuthorizationException: Bad credentials at Octokit.Connection.HandleErrors(IResponse response) at Octokit.Connection.d58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Octokit.Connection.d571.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Octokit.ApiConnection.<GetPage>d__411.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Octokit.ApiConnection.<>c__DisplayClass17_0`1.<b__0>d.MoveNext()

sarensw commented 4 years ago

I guess the problem ist that you are trying to access a non-top-level API. For these, you need an installation token. So after getting a GitHub client, you need to get another token for your installation similar to this:

var app = await m_gitHttpClient.GitHubApps.GetCurrent();
var installation = await m_gitHttpClient.GitHubApps.GetInstallationForCurrent(installationId);
var installationToken = await m_gitHttpClient.GitHubApps.CreateInstallationToken(installationId);

m_gitHttpClient = new GitHubClient(new ProductHeaderValue($"myApp-{installationId}"))
{
    Credentials = new Credentials(installationToken.Token)
};