Look up accountId of addon

Is there a simple way to look up the accountId of an addon?
I know the accountId is static once your addon is in the marketplace.

I’m looking for two ways:

  1. As a vendor, how can I look up the accountId of an addon in the marketplace settings?

  2. Acting programmatically, how can the addon look up its accountId during runtime (e.g. through a REST API call)?

  1. I don’t know.
  2. In issue-updated webhook you can use req.body.user.accountId to see who performed issue update (if that was your app then you have the ID you are looking for). This is what we check in our app but using REST API would be simpler to simply get it. There is an API endpoint to get plugin data AFAIR. It is worth to check if it returns the ID.

@jack Thank you. Your solution is for Jira, however we are on Confluence.

You don’t need to look up the accountId of the “addon user”, it’s the same for all tenants so you can hardcode it once you’ve deployed you app once.

@david2 Thanks for your answer. I understand that.
When I’m working on a new addon, that is not yet in the marketplace, I need to look up the accountId.

And it would be great if there was an “official” way to look up the accountId once an addon is in the marketplace.

Hi @marc,

Have you tried calling /rest/api/3/myself after the app receives the /installed event?

Regards,
Dugald

Hi @dmorrow ,
Thank you. I assume that would work for Jira, however I’m on Confluence.

Currently I’m looking up the accountId through searching for the name of the addon, but that feels backwards somehow.

Hi @mark,

For Confluence, can your app make a JWT call /wiki/rest/api/user/current after the app receives the /installed event? Non-user impersonation JWT calls act as the app so this call should return the details of the user associated with the app? The response JSON should include an accountId field and an accountType field.

Regards,
Dugald

1 Like

Hi @mark,

I did a little test of my solution and it works. The only small trick is that if you are doing it within your /installed handler, you need to return a 200 or 204 response code before asynchronously sending the request to /wiki/rest/api/user/current. This is to ensure the app is enabled before Confluence receives the REST request. The response will include an accountId field and an accountType field which will be app.

Regards,
Dugald

3 Likes

Hi @dmorrow Your help is very much appreciated!
I tried using the route you provided:

with the following code:

   app.get('/postinstall', addon.authenticate(), function(req, res) {
        // experiment to get the app accountId
        try {
            let httpClient = addon.httpClient(req);
            let url = '/wiki/rest/api/user/current';
            let accountid = '';
            httpClient.get(url, function(err, res, body) {
                if (err) {
                    hlogger.error('error calling /wiki/rest/api/user/current endpoint');
                }
                hlogger.debug('statusCode', {statusCode: res.statusCode});
                hlogger.debug('body', {body: body});
            });
        }
        catch (e) {
            let errorj = JSON.stringify(e);
            hlogger.error('error', {error: errorj});
        }
        res.render('postinstall');
    });

However I always get this response:

[debug] statusCode statusCode=403
....
<body><h1>
  HTTP Status 403 – Forbidden</h1>
<hr class=\"line\" /><p><b>Type</b> Status Report</p><p><b>Message</b>
 Request not in an authorized API scope</p><p><b>Description</b> The server understood the request but refuses to authorize it.</p><hr class=\"line\" /><h3>Apache Tomcat/7.0.91</h3></body>

My app has READ and WRITE as scopes as required in the documentation. Is there anything obvious I’m doing wrong? One difference is that I’m using the /postinstall route, but I think it should not make a difference.

Hi @dmorrow
After trying to apply your suggestion, I run into the problem that the API returns HTTP Status 403 – Forbidden.
Can you post your example code and the scopes the app would need in order to make the call? That would help a lot.

Hi @marc,

Is the /postinstall handler mapped from your app’s "lifecycle.enabled" descriptor property? If so, I think you just need to remove the /wiki prefix from your url.

Regards,
Dugald

@dmorrow Thank you, removing the /wiki prefix did it. Really appreciate your support :clap:

Code example for others:

    app.get('/postinstall', addon.authenticate(), function(req, res) {
        // experiment to get the app accountId
        try {
            let httpClient = addon.httpClient(req);
            let url = '/rest/api/user/current';
            let accountid = '';
            httpClient.get(url, function(err, res, body) {
                if (err) {
                    console.log('error calling /rest/api/user/current endpoint');
                }
                console.log('statusCode: '+JSON.stringify(res.statusCode));
                console.log('body: '+JSON.stringify(body));
            });
        }
        catch (e) {
            let errorj = JSON.stringify(e);
            console.log('error: '+errorj);
        }
        res.render('postinstall');
    });
1 Like