如何通过providerId,accessToken和secret获取providerUserId? (春季社交) [英] How to get providerUserId by providerId, accessToken and secret? (Spring Social)

查看:249
本文介绍了如何通过providerId,accessToken和secret获取providerUserId? (春季社交)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我可以通过providerId和providerUserId来标识用户.但是最初,我只有以下信息:

In my application I can identify user by providerId and providerUserId. But initially, I have only following information:

  • providerId,
  • accessToken,
  • 秘密.

因此,我需要通过此信息获取providerUserId .

我正在尝试使用以下代码:

I'm trying to use following code:

ConnectionData connectionData = newConnectionData(providerId, accessToken, secret);

ConnectionFactory<?> connectionFactory = connectionFactoryLocator.getConnectionFactory(providerId);
Connection<?> connection = connectionFactory.createConnection(connectionData);

if(connection.test()) {
    connection.sync();
} else {
    throw new AuthException();
}

return userEntityService.findOneByConnectionKey(connection.getKey());

但是问题是连接密钥没有初始化:providerUserId为null.

But problem is that connection key is not initialized: providerUserId is null.

在这种情况下如何获取?

推荐答案

有点晚了,但是,如果您遵循spring-social哲学",则会有一个UserConnection表.您可以查询providerUserId.

A bit late, however, if you are following the spring-social "philosophy", there is a UserConnection table. You can query it for providerUserId.

该模式位于JdbcUsersConnectionRepository.sql中:

-- This SQL contains a "create table" that can be used to create a table that JdbcUsersConnectionRepository can persist
-- connection in. It is, however, not to be assumed to be production-ready, all-purpose SQL. It is merely representative
-- of the kind of table that JdbcUsersConnectionRepository works with. The table and column names, as well as the general
-- column types, are what is important. Specific column types and sizes that work may vary across database vendors and
-- the required sizes may vary across API providers. 

create table UserConnection (userId varchar(255) not null,
providerId varchar(255) not null,
providerUserId varchar(255),
rank int not null,
displayName varchar(255),
profileUrl varchar(512),
imageUrl varchar(512),
accessToken varchar(512) not null,
secret varchar(512),
refreshToken varchar(512),
expireTime bigint,
primary key (userId, providerId, providerUserId));
create unique index UserConnectionRank on UserConnection(userId, providerId, rank);

这篇关于如何通过providerId,accessToken和secret获取providerUserId? (春季社交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆