2012-07-12 50 views
7

谷歌有一个OAuth2用户端here谷歌的OAuth2命令行示例

我completelly新到OAuth2,我想获得这个例子中工作前,我移动到OAuth2与我的应用程序集成的一个例子。我所做的是以下几点:

  1. 注册一个测试应用
  2. 获取客户端ID和客户端密钥
  3. 配置这些值转换成的client_secrets.json
  4. 运行测试程序:python moderator.py

该应用程序打开浏览器,我可以(作为用户)授权应用程序访问我的帐户。但是,谷歌抱怨像这样(400错误请求):

Error: redirect_uri_mismatch 
The redirect URI in the request: http://localhost:8080/ did not match a registered redirect URI 
Learn more 
Request Details 
from_login=1 
scope=https://www.googleapis.com/auth/moderator 
response_type=code 
access_type=offline 
redirect_uri=http://localhost:8080/ 
approval_prompt=auto 
as=-xxxxxxxxxxxxx 
pli=1 
client_id=xxxxxxxxxxx.apps.googleusercontent.com 
authuser=0 
hl=en 

我猜本地主机:8080是由moderator.py启动的内部Web服务器来。我的问题是:有人得到这个例子工作?我需要什么其他组件(Apache配置,DNS,...)

我很困惑与OAuth2和任何帮助将不胜感激。

回答

0

在OAuth 2.0中,redirect_uri参数通常在提供程序中注册。提供者还应该执行https-only redirect_uri。

您需要注册与谷歌在这里REDIRECT_URI:如果我的回答不是很精确https://code.google.com/apis/console/?pli=1#access

+3

我已经注册在谷歌API控制台我的测试应用程序,但我运行命令行应用程序(它被称为“命令行示例”)。我没有可以重定向到的URI。我需要在哪里或如何运行此示例,以便用户能够授权我的应用程序访问用户数据。这只是了解OAuth2如何工作的一个简单示例,但我无法使其运行。 – dangonfast 2012-07-12 19:52:56

0

也许尝试注册您的外部IP与谷歌(可能需要一些端口fowarding在您的路由器)?如果失败了,也许你可以使用Python的SimpleServer,注册你的IP并获得这个服务器来处理重定向。

0

你REDIRECT_URI设置为“http://localhost:8080/”,因为你通过一个默认的(我不知道怎么形容)标志参数run_flow(流动,储存,旗)

,如果你看一下定义run_flow()函数,你会发现这一点:

It presumes it is run from a command-line application and supports the 
following flags: 

    ``--auth_host_name`` (string, default: ``localhost``) 
     Host name to use when running a local web server to handle 
     redirects during OAuth authorization. 

    ``--auth_host_port`` (integer, default: ``[8080, 8090]``) 
     Port to use when running a local web server to handle redirects 
     during OAuth authorization. Repeat this option to specify a list 
     of values. 

    ``--[no]auth_local_webserver`` (boolean, default: ``True``) 
     Run a local web server to handle redirects during OAuth 
     authorization.