2012-07-12 60 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.