gpt4 book ai didi

java - Stormpath 无法检索用于谷歌登录的帐户对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:27 25 4
gpt4 key购买 nike

我绝对遵循了 google guide 到字母和 stormpath guide for integrating with google 也给字母

现在,用户可以成功登录 Google,但是 Stormpath 在尝试使用从 google 获得的授权代码检索 Stormpath Account 对象时在此行抛出异常;

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;
}

这是异常 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

请问我哪里做错了,这个问题的解决方案是什么?

编辑:

如果这有助于提供更多上下文,下面是我创建 Google 目录的方式:

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. 转到 https://console.developers.google.com/apis/credentials
  2. 确保“添加凭据”
  3. 记下“Client ID”和“Client secret”
  4. 添加一些“授权重定向 URI”,例如 http://localhost:8080/googleOauthCallback
  5. 登录https://api.stormpath.com
  6. 添加 Google Directory,提供正确的 IDSecretRedirect URI
  7. 现在,让我们以简单的方式获取 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 中获得的客户端 ID。

  8. 您将收到无法连接到服务器 错误,但这没关系,因为我们没有运行 Google 可以给我们回电的 Web 应用程序。这里重要的是在 URL 中。只需复制 code 值。它将类似于 4/tcHrwq4N1eah1rwotyCEaXq-yfxBOYrIAVe2_ouHTMQ

  9. 此代码将通过 Stormpath 检索 Google 帐户:

    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());

就这样吧

关于java - Stormpath 无法检索用于谷歌登录的帐户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com