Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a google user's domain has my marketplace app installed

When a user logs in, I want to check if their domain has my Marketplace app installed. It looks like this should in theory be possible with the Marketplace License API endpoints.

However, whenever I try to use the "Try it now" feature for the Customer License, User License or License Notification endpoints, I always get a 403 Forbidden with the message "Not authorized to access the application ID".

For example, if I try to query the LicenseNotification endpoint, I do the following:

Click on the Authorize toggle and click "Authorize" to authorize that scope for my logged-in user (which is the Google Apps Admin account which owns the application, btw).

For applicationId, I then enter the 12-digit "App Id" field from the Google Apps Marketplace SDK settings in the old developers console (Also known as Project Number from the new developers console app overview page).

When I click Execute I get the 403 "Not authorized to access the application ID". I have also tried using my Project Id (i.e. "my-app" from the Developers Console Overview page) in place of the Project Number/App Id, and get the same response.

Am I missing something here?

Alternatively, if someone knows of another way for the owner of a GAM App to query a list of domains which have it installed, that would be an ideal solution for me as well - I couldn't find anything like this.

like image 540
brettjonesdev Avatar asked Nov 29 '14 19:11

brettjonesdev


People also ask

How do I find Google marketplace?

Browse Google Workspace Marketplace apps The Google Workspace Marketplace is available at https://workspace.google.com/marketplace. Admins can also access the Google Workspace Marketplace from the Admin console: Sign in to your Google Admin console.

Does Google have a marketplace?

You can select and deploy software packages from the Cloud Marketplace page of the Google Cloud console. Cloud Marketplace offers many different products, and in some cases, offers a few variations of the same product; for example, Cloud Marketplace has multiple packages for WordPress.

How do I install Google Workspace Marketplace apps?

Install an app on your admin accountSign in using your administrator account (does not end in @gmail.com). Apps list. Click Add app to Admin Install list. Browse Google Workspace Marketplace and click the app that you want to install.


1 Answers

Okay, read some more and figured this out at last.

What I was missing was the fact that the authentication for the Licensing endpoints requires you to use a Service Account, not a regular user account. Which makes sense why the "Try it Now" feature on the documentation pages didn't work at all.

Unfortunately, we use PHP and the google-api-php-client does NOT yet have services for the Licensing APIs. However, the client project does show an example of using a service account instead of the normal user OAuth2 flow.

I used this example and stole a little bit of the source code from Resource.php's call method to call the Customer License endpoint to check to see if a domain has our app installed or not:

$privateKey = file_get_contents('path/to/private-key.p12');
$serviceAccountName = '[email protected]';

$cred = new \Google_Auth_AssertionCredentials(
    $serviceAccountName,
    array('https://www.googleapis.com/auth/appsmarketplace.license'),
    $privateKey
);
$client = new \Google_Client();
$client->setApplicationName('Apps_Marketplace_Licensing_Check');
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}


$url = \Google_Http_REST::createRequestUri(
    'appsmarket/v2/',
    'customerLicense/{appId}/{customerId}', [
        'appId' => ['location' => 'path', 'type' => 'string', 'value' => $appId], 
        'customerId' => ['location' => 'path', 'type' => 'string', 'value' => $domain]
    ]
);

$httpRequest = new \Google_Http_Request($url, 'GET');
$httpRequest->setBaseComponent($client->getBasePath());
$httpRequest = $client->getAuth()->sign($httpRequest);

/* returns JSON array */
$result = $client->execute($httpRequest);
$isDomainInstalled = ($result && isset($result['state']) && $result['state'] == 'ACTIVE');

Hopefully the folks over at the google-api-php-client project will eventually add a true service for these endpoints, but for now this workaround isn't too terribly painful.

like image 127
brettjonesdev Avatar answered Sep 19 '22 23:09

brettjonesdev