【发布时间】:2015-10-04 09:15:47
【问题描述】:
我们为 Google Apps Marketplace 创建了一个应用程序。我们的应用程序只有在为所有人安装时才有效。但问题是,一些客户为某些组织安装我们的应用程序,而不是每个人。我们想向这些客户显示特定消息,但问题是我们不知道我们的应用程序是为某些组织安装的,还是根本没有安装。因此,为某些组织安装了我们的应用程序的客户会收到一条消息,该消息是针对根本没有安装我们的应用程序的客户的。我们向他们展示了安装按钮,但是当他们再次安装我们的应用程序时没有任何反应,因为它已经安装了。我们希望向他们说明如何将我们的应用状态更改为“为所有人启用”。
我们如何检查我们的应用是否已为某些组织安装?我们从 Google 收到以下错误消息:
Failed to retrieve access token: {
"error" : "unauthorized_client",
"error_description" : "Unauthorized client or scope in request."
}
对于根本没有安装我们的应用程序的客户,我们收到的错误消息是一样的。
这是抛出异常的 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)
更新 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
【问题讨论】:
-
请详细解释“为组织安装”是什么意思!
-
@KlausD。请查看我更新的问题。
-
更新没有说清楚。
-
@Alex.S Google Apps 域的管理员,当他安装我们的应用程序时,他可以选择“为所有人开启”、“为选定的组织开启”或“关闭”。但是我们的应用只有在“所有人都可以使用”的情况下才有效
-
我也不明白这个问题。除了代码的sn-p之外,我还想看一个问题。我很惊讶它没有被标记。
标签: python google-apps google-apps-marketplace