title: How can we check if our app is installed for some organizations but not all of them?
body: <p>We created an application for Google Apps Marketplace. Our app works only if it's installed for everyone. But the problem is, some customers install our app for some organizations, not everyone. We want to display a specific message to those customers, but the problem is that we don't know if our app is installed for some organizations, or not installed at all. Therefore, customers who installed our app for some organizations get a message which is intended for customers who didn't install our app at all. We show them the install button but nothing happens when they install our app again, because it's already installed. We want to give them instructions how to change our app's status to "on for everyone".</p>
  
  <p>How can we check if our app is installed for some organizations? We get the following error message from Google:</p>
  
  <pre><code>Failed to retrieve access token: {
    "error" : "unauthorized_client",
    "error_description" : "Unauthorized client or scope in request."
  }
  </code></pre>
  
  <p>Which is the same error message we receive for cutomers who didn't install our app at all.</p>
  
  <p>This is the Python function who throws the exception:</p>
  
  <pre><code>  def _do_refresh_request(self, http_request):
      """Refresh the access_token using the refresh_token.
  
      Args:
        http_request: callable, a callable that matches the method signature of
          httplib2.Http.request, used to make the refresh request.
  
      Raises:
        AccessTokenRefreshError: When the refresh fails.
      """
      body = self._generate_refresh_request_body()
      headers = self._generate_refresh_request_headers()
  
      logger.info('Refreshing access_token')
      resp, content = http_request(
          self.token_uri, method='POST', body=body, headers=headers)
      if resp.status == 200:
        # TODO(jcgregorio) Raise an error if loads fails?
        d = simplejson.loads(content)
        self.token_response = d
        self.access_token = d['access_token']
        self.refresh_token = d.get('refresh_token', self.refresh_token)
        if 'expires_in' in d:
          self.token_expiry = datetime.timedelta(
              seconds=int(d['expires_in'])) + datetime.datetime.utcnow()
        else:
          self.token_expiry = None
        if self.store:
          self.store.locked_put(self)
      else:
        # An {'error':...} response body means the token is expired or revoked,
        # so we flag the credentials as such.
        logger.info('Failed to retrieve access token: %s' % content)
        error_msg = 'Invalid response %s.' % resp['status']
        try:
          d = simplejson.loads(content)
          if 'error' in d:
            error_msg = d['error']
            self.invalid = True
            if self.store:
              self.store.locked_put(self)
        except StandardError:
          pass
        raise AccessTokenRefreshError(error_msg)
  </code></pre>
  
  <p><strong>Update 1:</strong> in Apps > Marketplace apps, an app can be on for everyone, on for selected orgs or off. We need to know the status of our app.</p>
  
  <p><a href="https://i.stack.imgur.com/snXTI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/snXTI.png" alt="in Apps &gt; Marketplace apps"></a> </p>
  
  <p><strong>Update 2:</strong> I tried calling <code>check_general_access</code> but also when our application is uninstalled we receive True (Application has general access). This is after we confirmed that <code>check_access</code> returned False.</p>
  
  <pre><code>@staticmethod
  def check_access(admin_email):
      http = httplib2.Http()
      credentials = SignedJwtAssertionCredentials(
          SERVICE_EMAIL, 
          PRIVATE_KEY, 
          scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/ https://www.googleapis.com/auth/admin.directory.user.readonly', 
          sub=str(admin_email),
      )
      http = credentials.authorize(http)
      try:
          service = build(serviceName='admin', version='directory_v1', http=http)
          logging.info("Application has access to admin's %s domain" % (admin_email))
          return True
      except Exception as e:
          logging.info("Application does not have access to admin's %s domain (exception: %s)" % (admin_email, e.message))
          return False
  
  @staticmethod
  def check_general_access():
      http = httplib2.Http()
      credentials = SignedJwtAssertionCredentials(
          SERVICE_EMAIL,
          PRIVATE_KEY,
          scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/ https://www.googleapis.com/auth/admin.directory.user.readonly',
      )
      http = credentials.authorize(http)
      try:
          service = build(serviceName='admin', version='directory_v1', http=http)
          logging.info("Application has general access")
          return True
      except Exception as e:
          logging.info("Application does not have general access (exception: %s)" % e.message)
          return False
  </code></pre>