Comments (4)
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.
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.
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.
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)
- 🚀🚀🚀 TransferAllIssues -> parallel foreach async -> MAGIC :) 🚀🚀🚀 HOT 1
- Support for GitHub Discussions HOT 1
- Has this project been abandoned? HOT 9
- ResponseDeserializerException should have access to data HOT 3
- ConvertedToDiscussionEvent issue event type not found
- Select() is not supported when app configured with UWP Sideload HOT 10
- Nested pagination problem HOT 1
- Learn GraphQL
- Learn GraphQL
- Rename default branch name 'master' with 'main' HOT 3
- Update all libraries to use .NET Standard 2.0 HOT 1
- "Sequence contains no elements" exception is thrown when trying to call AllPages on a Search connection
- Getting a list of commits from a repo, fails when there is no commits/default branch HOT 1
- Share authorization with octokit.net HOT 2
- [BUG]: Mutation UpdateProjectV2ItemFieldValue fails with ProjectV2FieldValue has the wrong arguments error HOT 3
- [MAINT]: Project maintainance
- Unit Testing: How to bypass RunPage Deserializer?
- [BUG]: Sub-queries in GraphQL use C# type names not GraphQL entity names HOT 1
- [BUG]: EnablePullRequestAutoMergeInput failing using v0.2.1-beta with GHES 3.8.4 HOT 2
- [FEAT]: Provide direct access to all workflow runs through repository node HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from octokit.graphql.net.