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?
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.