我们如何检查我们的应用程序是否已安装用于某些组织而非所有组织?

Uri*_*Uri 25 python google-apps google-apps-marketplace

我们为Google Apps Marketplace创建了一个应用程序.我们的应用程序只有在为每个人安装时才有效.但问题是,有些客户为某些组织而不是每个人安装我们的应用程序.我们希望向这些客户显示特定消息,但问题是我们不知道我们的应用程序是为某些组织安装的,还是根本没有安装.因此,为某些组织安装我们的应用程序的客户会收到一条消息,该消息适用于根本没有安装我们的应用程序的客户.我们向他们展示了安装按钮,但是当他们再次安装我们的应用程序时没有任何反应,因为它已经安装.我们希望向他们提供如何将我们的应用状态更改为"适合所有人"的说明.

我们如何检查是否为某些组织安装了我们的应用程序?我们从Google收到以下错误消息:

Failed to retrieve access token: {
  "error" : "unauthorized_client",
  "error_description" : "Unauthorized client or scope in request."
}
Run Code Online (Sandbox Code Playgroud)

对于没有安装我们的应用程序的cutomers,我们收到的错误消息是相同的.

这是抛出异常的Python函数:

  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)
Run Code Online (Sandbox Code Playgroud)

更新1:在应用程序>市场应用程序中,可以为所有人启用应用程序,对于选定的组织或关闭应用程序.我们需要知道我们的应用程序的状态.

在应用>市场应用程序中

更新2:我尝试调用,check_general_access但是当我们的应用程序被卸载时,我们收到True(应用程序具有一般访问权限).这是在我们确认check_access返回False之后.

@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
Run Code Online (Sandbox Code Playgroud)

小智 -1

您可以每隔一小时左右添加一次 ping 呼叫某个端点。如果 ping 时间太长,他们可能会删除该应用程序