How to get providerUserId by providerId, accessToken and secret? (Spring Social)

纵然是瞬间 提交于 2019-12-13 07:27:35

问题


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

  • providerId,
  • accessToken,
  • secret.

Thus, I need to acquire providerUserId by this information.

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

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

How can I acquire it in this case?


回答1:


Generally, this code is intended to be used internally by Spring Social's connection framework (e.g., ConnectController, ConnectionRepository, ConnectionFactory, etc). Normally, you wouldn't use it directly unless you were looking to extend the framework or achieve something that the framework doesn't do for you.

Provider ID is determined by the connection factory being used. For example, the FacebookConnectionFactory defines it as "facebook". For Twitter it's "twitter". The value isn't terribly important, except that it be (1) consistently used for all connections against the same provider and (2) it be unique among all providers. Generally, it's good to just use the provider's name in all lowercase.

The access token is obtained by going through the OAuth "dance" (e.g., a series of redirects and prompts to obtain user authorization). ConnectController handles this for you...so does ProviderSignInController. If the token is an OAuth2 token, there will be no secret. If it's an OAuth 1.0(a) token, then you'll be given the secret along with the token at the end of the dance.




回答2:


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

The schema is in 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);


来源:https://stackoverflow.com/questions/33581432/how-to-get-provideruserid-by-providerid-accesstoken-and-secret-spring-social

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!