我正在尝试在从 Google Apps 市场安装的应用程序中检查用户是否是其 Google Apps 域的管理员。
我将其添加到 manifest.xml 中:
<Scope id="Provisioning API">
<Url>https://apps-apis.google.com/a/feeds/user/#readonly</Url>
<Reason>This application can list domain users to give them permissions.</Reason>
</Scope>
然后我设置了一个测试处理程序来让它工作:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import gdata.alt.appengine
import gdata.apps.service
import gdata.auth
# App id, key and secret from the Google Apps Marketplace.
APPLICATION_ID = 'XXX'
CONSUMER_KEY = 'XXX'
CONSUMER_SECRET = 'XXX'
class SetupHandler(webapp.RequestHandler):
def get(self, *args):
# The domain where this app is installed.
domain = 'my_customer_domain.com'
# A username to check.
username = 'webmaster'
sig_method = gdata.auth.OAuthSignatureMethod.HMAC_SHA1
service = gdata.apps.service.AppsService(source='tipfy-com',
domain=domain)
service.SetOAuthInputParameters(sig_method, CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
two_legged_oauth=True,
requestor_id=APPLICATION_ID)
service.ssl = True
service.debug = True
gdata.alt.appengine.run_on_appengine(service)
lookup_user = service.RetrieveUser(username)
if lookup_user.login.admin == 'true':
res = username + ' is an admin.'
else:
res = username + ' is not an admin.'
self.response.out.write(res)
app = webapp.WSGIApplication([
('/.*', SetupHandler),
], debug=True)
def main():
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
但我收到 401 响应(“未知授权 header ”)。我不知道我做错了什么或如何进一步调试它。
- list 条目是否正确?
- 拆分
user.email()
可以得到用户的用户名和域吗? (我对其进行了调试,在我的情况下是:我得到了“网站管理员”和“example.com”,这是安装该应用程序的用户和 Google Apps 域)。
我错过了什么?
编辑:出于某种原因,管理面板没有请求授予对所提供范围的访问权限。在我授予它之后,上面的代码起作用了。因此,将其作为一个工作示例!
最佳答案
您不必重新添加您的应用程序,只需确保在您的 GoogleApps 管理仪表板中的应用程序设置中,您“授予访问权限”并且数据访问权限是“授予”。否则只需授予该访问权限。
拆分 user.email()
对我来说就像一个魅力,因为本地主机测试中的 user.nickname()
包含完整的电子邮件,而不像生产(它在哪里包含用户名)。
确保请求的用户是管理员。
关于google-app-engine - 市场应用 + 配置 API : check if user is admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6049427/