2013-03-26 134 views
1

我一直在使用Google Analytics作爲我的網站,我有created in weebly。我正在使用Google API以編程方式實現整個事情。OAuth 2.0中redirect_uri的混淆

我的OAuth的流動過程中面臨的問題是,我收到此錯誤:

Error: redirect_uri_mismatch 
The redirect URI in the request:localhost:34190/Callback did not match a registered redirect URI 

     Request Details 
     scope=https://www.googleapis.com/auth/analytics.readonly 
     response_type=code 
     access_type=online 
     redirect_uri=local_host:34190/Callback 
     display=page 
     client_id={CLIENT_ID}.apps.googleusercontent.com 

我的谷歌API控制檯配置爲:

Redirect URI: localhost/oauth2callback 
JavaScript origins: localhost 

爲什麼地球上redirect_urilocalhost:34190/Callback當我把它設置爲:http://mya.com/oauth2callback?這都是我爲openauth書面

代碼:

public static void main(String[] args) throws Exception { 
    Analytics analytics = initializeAnalytics(); 
} 

private static Analytics initializeAnalytics() throws Exception { 
    Credential credential = authorize(); 
} 

private static Credential authorize() throws Exception { 
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
     JSON_FACTORY, Testcode.class.getResourceAsStream("/client_secrets.json")); 
    FileCredentialStore credentialStore = new FileCredentialStore(
     new File(System.getProperty("user.home"), ".credentials/analytics.json"), 
     JSON_FACTORY); 
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
     HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, 
     Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).setCredentialStore(
      credentialStore).build(); 
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) 
        .authorize(clientSecrets.getDetails().getClientId()); 
    }  

我怎樣才能擺脫這種錯誤的?

回答

4

因此,您的網站告訴Google將localhost:34190/Callback用作重定向URI。但是您已經對Google服務器說過,只有當您的應用將localhost/oauth2callback指定爲重定向URI時,他纔會接受請求。

最簡單的解決將是谷歌設置控制檯如下:

Redirect URI: http://localhost:34190/Callback 

無論如何,你都在談論你想重定向到http://mya.com/oauth2callback

在這種情況下,你應該改變用戶點擊該鏈接在您的網站指定這個

[...]&redirect_uri=http://mya.com/oauth2callback[...] 

,然後在谷歌控制檯進行設置:

Redirect URI: http://mya.com/oauth2callback 
+0

感謝揚!!!!,我將google api控制檯設置爲localhost:34190,但它每次都在更改,甚至mya.com在我的主機文件中解析爲本地主機。 – rampuriyaaa 2013-03-26 12:12:20

+0

如果我的答案確實有幫助,那麼將其標記爲*接受*會很好。 – 2013-03-26 14:16:00

+0

Hey Jan !!!! ...你發佈的信息我已經知道了,我從其他網站上找到了。我的問題仍然沒有解決 – rampuriyaaa 2013-03-27 03:33:12