google-app-engine - 市场应用 + 配置 API : check if user is admin

标签 google-app-engine provisioning google-apps google-apps-marketplace

我正在尝试在从 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/

相关文章:

python - 任务队列停止工作

chef-infra - Chef 加载新 rpm 和安装包的正确方法

google-app-engine - 同时使用 2 足 OAuth 和 3 足 OAuth 的 Python GAE 应用程序

ios - 新的 iOS 团队成员 : no valid signing identity

api - 用于在 Google Apps 域中获取最大许可数的 Google API

javascript - getCreators() 类型错误

javascript - 如何向 Chrome 扩展程序发送数据?

java - 带有 Gradle 的谷歌云存储 Java 客户端库

google-app-engine - 无法使用 Android Studio Beta (0.8.1) 导入 App Engine DatastoreService

mongodb - 在 mongodb 启动后让 Chef 执行一个 mongodb 脚本