Google OAuth与本地开发

4 投票
2 回答
1388 浏览
提问于 2025-04-15 20:12

我正在尝试使用谷歌的OAuth来导入用户的联系人。为了获取你应用的消费者密钥和秘密密钥,你需要在https://www.google.com/accounts/ManageDomains上验证你的域名。谷歌只允许使用没有端口的域名。我想在本地测试和构建这个应用,所以通常(像Facebook、LinkedIn的应用)我会使用反向SSH隧道,比如http://6pna.com:30002

有没有人用过隧道和谷歌的OAuth?这样能行吗?到目前为止,我只是验证了我应用的域名,但我的请求是通过隧道发出的(不同的域名),所以OAuth失败了(尽管我能成功访问谷歌并授权我的应用)。

有没有什么建议或者提示?谢谢!

2 个回答

3

我只是使用了官方的 gdata Google 认证库,链接在这里:http://code.google.com/p/gdata-python-client

下面是一些代码

    google_auth_url = None
    if not current_user.gmail_authorized:
        google = gdata.contacts.service.ContactsService(source=GOOGLE_OAUTH_SETTINGS['APP_NAME'])
        google.SetOAuthInputParameters(GOOGLE_OAUTH_SETTINGS['SIG_METHOD'], GOOGLE_OAUTH_SETTINGS['CONSUMER_KEY'],
                                      consumer_secret=GOOGLE_OAUTH_SETTINGS['CONSUMER_SECRET'])
        if not request.vars.oauth_verifier:
            req_token = google.FetchOAuthRequestToken(scopes=GOOGLE_OAUTH_SETTINGS['SCOPES'],
                          oauth_callback="http://"+request.env.http_host+URL(r=request,c='default',f='import_accounts'))
            session['oauth_token_secret'] = req_token.secret
            google_auth_url = google.GenerateOAuthAuthorizationURL()
        else:
            oauth_token = gdata.auth.OAuthTokenFromUrl(request.env.request_uri)
            if oauth_token:
                oauth_token.secret = session['oauth_token_secret']
                oauth_token.oauth_input_params = google.GetOAuthInputParameters()
                google.SetOAuthToken(oauth_token)
                access_token = google.UpgradeToOAuthAccessToken(oauth_verifier=request.vars.oauth_verifier)
                # store access_tonen

        #google.GetContactsFeed() # do the process or do it in ajax (but first update the user)
4

经过反复尝试,我发现请求的域名其实并不重要。

撰写回答