Giter Club home page Giter Club logo

Comments (4)

StanleyGoldman avatar StanleyGoldman commented on June 10, 2024 1

Hey @MartinDawson I personally just started playing with GitHub Apps, in another project.
I haven't tried to use this with the GitHub Apps APIs, but I will want to be able to soon.

from octokit.graphql.net.

plucked avatar plucked commented on June 10, 2024 1

I solved it by using the normal API to get the installation token. Here some sample code which works on a single repo (you need some logical changes if this should be able to work on all installations) and uses the nuget packages:

GitHubJwt
Octokit
Octokit.GraphQL

    public static class StaticData {
        public static string Repository = "asdf";
        public static string Organization = "asdf";
        public static string AppName = "asdf";
        public static int AppId = 123456;
    }
    
    public static class GitHubClientManager {
        private static TimeSpan jwtExpireTime = TimeSpan.FromMinutes(10);
        private static AccessToken installationToken = null;
        
        private static GitHubClient client = null;
        private static Connection graphQLConnection = null;

        public static async Task<GitHubClient> GetClient() {
            // reuse an existing client when the token it was issues with is still valid
            if (client != null && installationToken != null && DateTimeOffset.UtcNow < installationToken.ExpiresAt) {
                return client;
            }

            // 1. first we authenticate as a app
            // 2. then we need to get the installation id of this app
            // 3. with the installation id we can create an access token
            var jwtToken = CreateJWTToken();
            client = new GitHubClient(new ProductHeaderValue(StaticData.AppName)) {Credentials = new Credentials(jwtToken, AuthenticationType.Bearer)};
            var installation = await client.GitHubApps.GetRepositoryInstallationForCurrent(StaticData.Organization, StaticData.Repository);
            installationToken = await client.GitHubApps.CreateInstallationToken(installation.Id);
            client = new GitHubClient(new ProductHeaderValue(StaticData.AppName)) {Credentials = new Credentials(installationToken.Token, AuthenticationType.Bearer)};
            return client;
        }

        public static async Task<Connection> GetGraphQLConnection() {
            // reuse an existing client when the token it was issues with is still valid
            if (graphQLConnection != null && installationToken != null && DateTimeOffset.UtcNow < installationToken.ExpiresAt) {
                return graphQLConnection;
            }

            // creating a new client will make sure we have an access token 
            await GetClient();
            graphQLConnection = new Connection(new Octokit.GraphQL.ProductHeaderValue(StaticData.AppName), installationToken.Token);
            return graphQLConnection;
        }

        private static string CreateJWTToken() {
            // create a new token
            var generator = new GitHubJwt.GitHubJwtFactory(
                    new GitHubJwt.FilePrivateKeySource("adsf.pem"),
                    new GitHubJwt.GitHubJwtFactoryOptions {
                            AppIntegrationId = StaticData.AppId,
                            ExpirationSeconds = (int)jwtExpireTime.TotalSeconds
                    });
            return generator.CreateEncodedJwtToken();
        }
    }
`

from octokit.graphql.net.

MartinDawson avatar MartinDawson commented on June 10, 2024

Ah, thanks. I'll migrate to OAuth apps for now actually as we can use webhooks with them it turns out.

If this issue could stay open then I could switch back to GitHub Apps when it's fixed

Thanks.

from octokit.graphql.net.

p-bojkowski avatar p-bojkowski commented on June 10, 2024

Another version of GetClient():

       private static async Task<GitHubClient> GetClient()
       {
           var clientIsOk = client != null;
           var installationTokenIsOk = installationToken != null;
           var tokenIsNotExpired = installationToken != null && DateTimeOffset.UtcNow < installationToken.ExpiresAt;

           if (clientIsOk &&
               installationTokenIsOk &&
               tokenIsNotExpired)
           {
               // reuse an existing client when the token it was issues with is still valid
               return client;
           }

           // A time based JWT token, signed by the GitHub App's private certificate
           var jwtToken = CreateJWTToken();

           // Use the JWT as a Bearer token
           var appClient = new GitHubClient(new ProductHeaderValue(StaticData.AppName))
           {
               Credentials = new Credentials(jwtToken, AuthenticationType.Bearer)
           };

           // Get the current authenticated GitHubApp
           var app = await appClient.GitHubApps.GetCurrent();

           // Get a list of installations for the authenticated GitHubApp
           var installations = await appClient.GitHubApps.GetAllInstallationsForCurrent();

           // Get a specific installation of the authenticated GitHubApp by it's installation Id
           if (installations != null)
           {
               var allInstallationsForCurrent = await appClient.GitHubApps.GetAllInstallationsForCurrent();

               var installation = allInstallationsForCurrent.FirstOrDefault();

               // Create an Installation token for Installation installation.Id
               if (installation != null)
                   installationToken = await appClient.GitHubApps.CreateInstallationToken(installation.Id);
               if (installationToken != null)
                   client = new GitHubClient(new ProductHeaderValue(StaticData.AppName))
                       {Credentials = new Credentials(installationToken.Token, AuthenticationType.Bearer)};
           }

           return client;
       }

And some Test Code :)

using static Octokit.GraphQL.Variable;


       private async Task UseCase1()
       {
           var connection = await _currentGitHubClientManager.GetGraphQlConnection();

           var query = new Query()
               .RepositoryOwner(Var("owner"))
               .Repository(Var("name"))
               .Select(repo => new
               {
                   repo.Id,
                   repo.Name,
                   repo.Owner.Login,
                   repo.IsFork,
                   repo.IsPrivate,
               }).Compile();

           var vars = new Dictionary<string, object>
           {
               { "owner", "octokit" },
               { "name", "octokit.graphql.net" },
           };

           var result = await connection.Run(query, vars);
       }

from octokit.graphql.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.