2015-12-04 64 views
3

我已經完全遵循google guide的字母和stormpath guide for integrating with google還字母Stormpath無法檢索帳戶對象的谷歌登錄

現在,用戶可以通過谷歌成功登錄,但在嘗試使用從google獲得的授權代碼檢索Stormpath Account對象時,Stormpath會在此行上引發異常;

ProviderAccountResult result = application.getAccount(request); 

下面是代碼片段;

public static Account getAccount(String codeOrToken, AuthType authType){ 
    try{ 
     if(getDirectory() != null){ 
      ProviderAccountRequest request = null; 
      switch(authType){ 
       case AUTH_CODE: 
        request = Providers.GOOGLE.account().setCode(codeOrToken).build(); 
        break; 
       case ACCESS_TOKEN: 
        request = Providers.GOOGLE.account().setAccessToken(codeOrToken).build(); 
        break; 
       default: 
        break; 
      } 
      Application application = AuthUtil.getApplication(); 
      ProviderAccountResult result = application.getAccount(request); 
      Account account = result.getAccount(); 
      account.getCustomData().put("isNew", result.isNewAccount()); 
      return account; 
     } 
    }catch(Exception ex){ 
     ex.printStackTrace(); 
    } 
    return null; 
} 

這裏是Exception StackTrace;

16:45:02,170 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) com.stormpath.sdk.resource.ResourceException: HTTP 400, Stormpath 7200 (http://docs.stormpath.com/errors/7200): Stormpath was not able to complete the request to Google: this can be caused by either a bad Google directory configuration, or the provided account credentials are not valid. Google error message: 400 Bad Request 
16:45:02,172 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultDataStore.execute(DefaultDataStore.java:492) 

16:45:02,173 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultDataStore.access$000(DefaultDataStore.java:67) 

16:45:02,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultDataStore$2.filter(DefaultDataStore.java:390) 

16:45:02,175 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:47) 

16:45:02,176 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.ProviderAccountResultFilter.filter(ProviderAccountResultFilter.java:31) 

16:45:02,177 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,178 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.api.ApiKeyQueryFilter.filter(ApiKeyQueryFilter.java:74) 

16:45:02,180 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,181 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.cache.WriteCacheFilter.filter(WriteCacheFilter.java:80) 

16:45:02,184 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,184 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.cache.ReadCacheFilter.filter(ReadCacheFilter.java:62) 

16:45:02,185 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,186 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.api.DecryptApiKeySecretFilter.filter(DecryptApiKeySecretFilter.java:62) 

16:45:02,187 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,188 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.EnlistmentFilter.filter(EnlistmentFilter.java:42) 

16:45:02,189 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultFilterChain.filter(DefaultFilterChain.java:52) 

16:45:02,189 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultDataStore.save(DefaultDataStore.java:411) 

16:45:02,190 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.ds.DefaultDataStore.create(DefaultDataStore.java:322) 

16:45:02,191 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.provider.ProviderAccountResolver.resolveProviderAccount(ProviderAccountResolver.java:46) 

16:45:02,192 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at com.stormpath.sdk.impl.application.DefaultApplication.getAccount(DefaultApplication.java:325) 

16:45:02,193 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at ng.ajo.socials.Google.getAccount(Google.java:79) 

16:45:02,193 ERROR [stderr] (http-localhost-127.0.0.1-8080-5) at ng.ajo.server.SocialsServlet.doPost(SocialsServlet.java:81) 

//... The rest omitted for brevity 

請什麼可能我是錯的做有什麼能解決這個問題呢?

編輯:

如果這有助於提供更多的背景,這裏是我如何創建我的谷歌目錄:

public class Google { 

    public static Directory getDirectory(){ 
     try{ 
      Directory directory = DirectoryUtil.getGoogleDirectory(); 

      //if the google directory does not exist CREATE it 
      if(directory == null){ 
       Client client = AuthUtil.getClient(); 
       directory = client.instantiate(Directory.class); 
       directory.setName(dirName); 
       directory.setDescription(dirDescription); 

       CreateDirectoryRequest request = Directories.newCreateRequestFor(directory) 
          .forProvider(Providers.GOOGLE.builder() 
            .setClientId(appID) 
            .setClientSecret(appSecret) 
            .setRedirectUri(redirectURI) 
            .build() 
          ).build(); 

       Tenant tenant = client.getCurrentTenant(); 
       directory = tenant.createDirectory(request); 
       Application application = AuthUtil.getApplication(); 
       application.addAccountStore(directory.getHref()); 
      } 

      return directory; 

     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 
} 

當我登錄到我的Stormpath Web控制檯我可以看到,目錄已經創建,確實存在與所有配置配置... 現在,這整個事情應該工作,但它不... ...仍然讓我感到困惑!

回答

1
  1. 轉到https://console.developers.google.com/apis/credentials
  2. 確保您 「添加憑據」 「客戶端ID」 和 「客戶端祕密」
  3. 採取評說添加一些 「授權重定向的URI」 像http://localhost:8080/googleOauthCallback
  4. 登錄https://api.stormpath.com
  5. 添加Google目錄,提供正確的ID,SecretRedirect URI
  6. 現在,讓我們以簡單的方式獲取Google代碼(無需創建Web項目)。在瀏覽器中打開此URL;出現提示時,請選擇您要使用的Gmail帳戶。

    https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXX 
    &response_type=code 
    &scope=openid%20email 
    &redirect_uri=http://localhost:8080/googleOauthCallback 
    

    注:XXXXXXX必須由您在步驟3中得到

  7. 你會得到一個錯誤cannot connect to server客戶端ID被替換,但是這是確定,因爲我們沒有運行Web應用程序,其中谷歌可以撥打我們回來。這裏重要的是在URL中。只需複製code值。這將是像4/tcHrwq4N1eah1rwotyCEaXq-yfxBOYrIAVe2_ouHTMQ

  8. 此代碼將通過Stormpath獲取谷歌帳戶:

    Client client = Clients.builder().build(); 
    
    Application application = client.getResource(applicationHref, Application.class); 
    
    ProviderAccountRequest request = Providers.GOOGLE.account() 
         .setCode(code) //where code is the value we obtained in step 8 
         .build(); 
    
    ProviderAccountResult result = application.getAccount(request); 
    System.out.println("Account Email: " + result.getAccount().getEmail()); 
    ProviderData providerData = result.getAccount().getProviderData(); 
    System.out.println("Access Token: " + ((GoogleProviderData)providerData).getAccessToken()); 
    

這一切......

+0

嗨@jstack,感謝resppnse。在我的應用程序中,我已經完成了所有這些事情......我已經很好地獲得了授權代碼..​​....一切正常,直到那行'ProviderAccountResult result = application.get account(request);'...也許它可能與我如何獲得我的客戶端或應用程序對象有關,我不知道,即使這可能是原因似乎很奇怪,但我仍然會嘗試像我之前展示的那樣做,回覆發生了什麼。謝謝。 – CodeBurner

+0

檢查:1)Stormpath應用程序將Google Directory作爲帳戶存儲。 2)你傳遞的代碼(不是代碼中的accessToken(String)方法)3)如果你有多個Google App,請確保你在Stormpath中配置的目錄與你生成代碼的Google應用程序相同對於 – mario

+0

hi @jstack,我已經完成了所有這些事情,但沒有成功。事實上,如果你看看我在問題中提供的代碼,你會發現我正在做你正在描述的內容。我的''AuthUtil.getApplication();'正是這個'client.getResource(applicationHref,Application.class);'。當我在Eclipse中進行調試時,發現(1.)'application'對象被正確地檢索到(2.)我有一個來自google的'code',看起來像這樣''4/sz8XzIk5lPbgaQB3hNVg2M6pH06BdLC0G2IPdETujWM'(3.)'request' Object (*)PROBLEM是'application.getAccount(request)'仍然拋出異常; – CodeBurner