-
Feature RequestIs your feature request related to a problem? Please describe. I'd like to interact with GitHub from one of the HTTP routes exposed, e.g. open a new pull request when a POST is sent to the Maybe it's already possible at the moment but the documentation I've found only talks about I see exists an undocumented router.post('/new', async (req, res, next) => {
const client = await app.auth(Number(process.env.APP_ID), app.log)
client.git
.getRef({
owner: "shaftoe",
repo: "personal-website",
ref: "heads/master",
})
.then(response => {
app.log.error("response:" + response)
res.send(response)
})
.catch(error => {
app.log.error("error:" + error)
res.send(error)
})
}) I receive this error: "ERROR (app): error:Error: secretOrPrivateKey must have a value" but I don't see any way for setting that right. Describe the solution you'd like It would be great to have an easy way to get an authenticated |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Meanwhile I've worked around this following your recommendation here: #1324 (comment). The following seems to work ok: const token = await client.apps.createInstallationAccessToken({installation_id})
const { ProbotOctokit } = require("probot")
const client = new ProbotOctokit({
auth: "token " + token.data.token,
log: app.log.child({ name: "my-github" }),
})
client.git.getRef({
owner: "shaftoe",
repo: "personal-website",
ref: "heads/master"
}) |
Beta Was this translation helpful? Give feedback.
-
Want to add my 2 cents here, thanks to gathering info from this discussion I think I have a "ready-to-copy" code module.exports = ({ app, getRouter }) => {
...
router.post("/foo", async (req, res) => {
const octokit = await getAuthenticatedGithubClient(app);
...
});
}; const getAuthenticatedGithubClient = async (app) => {
const findOurInstallationId = (installations) => {
const { id } = installations.find(({ app_id }) => app_id === /*APP_ID*/); // read from .env
return id;
};
const client = await app.auth();
const installations = await client.paginate(client.apps.listInstallations);
const authenticatedClient = await app.auth(findOurInstallationId(installations));
return authenticatedClient;
}; Am I missing something? is there a better/easier way to do this? Hope this helps someone :) |
Beta Was this translation helpful? Give feedback.
app.auth(id)
will return an octokit client with installation authentication. Theid
needs to be an installation ID, not the app ID.app.auth()
will give you the global instance with app authentication, which you can use to iterate through installations and you can use to create new installation access tokens.