【问题标题】:Google hybrid ( openid + oauth) protocol doesn`t work for 127.0.0.1Google 混合(openid + oauth)协议不适用于 127.0.0.1
【发布时间】:2012-05-15 17:26:30
【问题描述】:

我正在尝试实现谷歌混合协议(oauth over openid)。 谷歌不要求oauth许可的问题(尝试使用gmail),只要求openid。 我在 google api 控制台注册:

Client ID for web applications
Client ID: 248141267047.apps.googleusercontent.com
Email address:248141267047@developer.gserviceaccount.com
Client secret: 
Redirect URIs:  http://127.0.0.1:8000/oauth2callback
JavaScript origins:     http://127.0.0.1:8000

这是我生成openid url的python代码:

class OpenIDOAuthRequest(Extension):

    ns_alias = 'oauth'

    def __init__(self, consumer, scope, ns_uri=None):
        Extension.__init__(self)
        self.consumer = consumer
        self.scope = scope
        self.ns_uri = ns_uri or oauth_ns_uri

    def getExtensionArgs(self):
        return {
            'consumer': self.consumer,
            'scope': ' '.join(self.scope),
        }


def google():
        #define google openid url
        openid_session = {}
        openid_store = filestore.FileOpenIDStore('.')
        consumer = Consumer(openid_session, openid_store)
        openid = u"https://www.google.com/accounts/o8/id"
        URLS = {
            'ax_last': "http://axschema.org/namePerson/last",
            'ax_first': "http://axschema.org/namePerson/first",
            'ax_email': "http://axschema.org/contact/email",
            "country":"http://axschema.org/contact/country/home",
            "timezone":"http://axschema.org/pref/timezone",
            "language":"http://axschema.org/pref/language",
            "person":"http://axschema.org/namePerson",
        }
        #defining what fields we're going to get
        ax_request = ax.FetchRequest()
        for k,v in URLS.iteritems():
            ax_request.add(ax.AttrInfo(v, required = True))
        oa = OpenIDOAuthRequest("248141267047.apps.googleusercontent.com",["https://mail.google.com/",])
        try:
            authrequest = consumer.begin(openid)
        except DiscoveryFailure, e:
            print e
            print "some errror happened"
        else:
            authrequest.addExtension(ax_request)
            authrequest.addExtension(oa)



        redirecturl = authrequest.redirectURL("http://127.0.0.1:8000",
            return_to = "http://127.0.0.1:8000/oauth2callback",
            immediate=False)
        print redirecturl

它会生成以下网址:

https://accounts.google.com/o/openid2/auth?openid.assoc_handle=AMlYA9Vr6Biwp-rCAr4TLbf8CtItR-zr3bs0LT7oYQ3Pakg93ivCS_6C&openid.ax.mode=fetch_request&openid.ax.required=ext0,ext1,ext2,ext3,ext4,ext5,ext6&openid.ax.type.ext0=http://axschema.org/contact/email&openid.ax.type.ext1=http://axschema.org/namePerson&openid.ax.type.ext2=http://axschema.org/namePerson/first&openid.ax.type.ext3=http://axschema.org/pref/timezone&openid.ax.type.ext4=http://axschema.org/pref/language&openid.ax.type.ext5=http://axschema.org/contact/country/home&openid.ax.type.ext6=http://axschema.org/namePerson/last&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.mode=checkid_setup&openid.ns=http://specs.openid.net/auth/2.0&openid.ns.ax=http://openid.net/srv/ax/1.0&openid.ns.oauth=http://specs.openid.net/extensions/oauth/1.0&openid.oauth.consumer=248141267047.apps.googleusercontent.com&openid.oauth.scope=https://mail.google.com/&openid.realm=http://127.0.0.1:8000&openid.return_to=http://127.0.0.1:8000/oauth2callback?janrain_nonce%3D2012-05-01T22%253A50%253A33ZUW7vcj

它有所有必要的扩展。但是,如果我访问此网址,它不会询问我是否允许使用 gmail。我也在比较来自 sanebox.com 的类似 url。它按预期工作,请求 gmail 的许可。但我没有看到为什么他们的网址有效而我的网址无效的任何本质区别。此外,我将网址中的 127.0.0.1 替换为 sanebox 网址,其他部分保持不变。而且......现在它正在请求gmail的许可。切换回 127.0.0.1 - 停止询问。这是 sanebox 网址:

https://accounts.google.com/o/openid2/auth?openid.assoc_handle=AMlYA9UV4Ud714HHaFJ0fpItabA8v-zw0QuReEPcn61ilJzyFrFia5PO&openid.ax.mode=fetch_request&openid.ax.required=ext0,ext1,ext2,ext3,ext4,ext5,ext6&openid.ax.type.ext0=http://axschema.org/pref/timezone&openid.ax.type.ext1=http://axschema.org/contact/country/home&openid.ax.type.ext2=http://axschema.org/pref/language&openid.ax.type.ext3=http://axschema.org/namePerson/last&openid.ax.type.ext4=http://axschema.org/namePerson/first&openid.ax.type.ext5=http://axschema.org/namePerson&openid.ax.type.ext6=http://axschema.org/contact/email&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.mode=checkid_setup&openid.ns=http://specs.openid.net/auth/2.0&openid.ns.ax=http://openid.net/srv/ax/1.0&openid.ns.oauth=http://specs.openid.net/extensions/oauth/1.0&openid.ns.sreg=http://openid.net/extensions/sreg/1.1&openid.oauth.consumer=www.sanebox.com&openid.oauth.scope=https://mail.google.com/+http://www.google.com/m8/feeds&openid.realm=https://www.sanebox.com/&openid.return_to=https://www.sanebox.com/users?_method%3Dpost%26open_id_complete%3D1

那么我错过了什么?如果我在 api 控制台中注册了这个 url,为什么它不适用于 127.0.0.1。它在 openid 上运行良好。并且在没有 openid 的情况下与 oauth 本身一起工作得很好。但是现在当我尝试在 openid 上使用 oauth 时,它不会要求我获得 gmail 的许可。

【问题讨论】:

    标签: python oauth gmail openid google-openid


    【解决方案1】:

    我已经运行了 PHP、ASP、DOT NET 代码,它们运行得非常好,因此可以确保 python 应用程序也能正确运行。您必须更改默认应用程序。首先运行他们提供的默认应用程序并尝试更改。如果默认应用程序不起作用,那么也可以向谷歌打开错误报告。

    【讨论】:

    • 问题仅在于使用 127.0.0.1 运行它。我最终将所需的域添加到主机文件。并且默认应用没有t exist for hybrid protocol for python. Only lib for openid that im 已经在使用
    • @Aldarund IT 即使在本地主机上也为我工作,即 127.0.0.1 对我来说
    • 我贴了两个链接,你可以尝试打开它们。与 localhost 的链接不会要求您访问 gmail,而与普通域的链接会这样做。链接之间的唯一区别是域。
    • 这两个我注意到了,1.发送请求的代码中你的客户端密码在哪里。 2. 勺子定义不正确。 openid ns claim_id 身份模式 oauth 范围领域在哪里。请先阅读 Open Id 规范,然后尝试实现相同
    • 1.客户端密码稍后会出现,而不是在我们生成 url 时。 2. 范围定义正确.. 此代码 100% 工作。它现在可以在真实站点上正常运行。
    猜你喜欢
    • 2010-10-23
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多