1

I am composing spring-social. When I click the / signin / facebook button:

Exception while completing OAuth 2 connection: Org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API was created without authorization.

I have encountered this error.
As a result of debugging, facebook access token seems to be well received.

An error occurs in this part after normal login of Facebook.
ConnectSupport class:

AccessGrant accessGrant = connectionFactory.getOAuthOperations().exchangeForAccess(code, callbackUrl(request), null);

This is my composition.

SocialConfig:

@Inject
private DataSource dataSource;

@Autowired
UserRepository userRepository;

@Autowired
PostSocialSignInAdapter postSocialSignInAdapter;

@Override
public UserIdSource getUserIdSource() {
    return new CUserIdSource();
}

@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
    String appKey = "4123213121231231212331";
    String appSecret = "512312312lkjj1232kj3l1jl213";

    cfConfig.addConnectionFactory(new FacebookConnectionFactory(appKey, appSecret));
}

@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
            connectionFactoryLocator, Encryptors.noOpText());
    repository.setConnectionSignUp(new SocialImplicitSignUp(userRepository));
    return repository;
}

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository repository) {
    Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class);
    return connection != null ? connection.getApi() : null;
}

@Bean
public ProviderSignInController providerSignInController(ConnectionFactoryLocator connectionFactoryLocator,
        UsersConnectionRepository usersConnectionRepository) {
    ProviderSignInController providerSignInController = new ProviderSignInController(connectionFactoryLocator,
            usersConnectionRepository, postSocialSignInAdapter);
    providerSignInController.setSignUpUrl("/");
    return providerSignInController;
}

UserRepository interface

public interface UserRepository extends JpaRepository<User, String> {
   public User findByusername(String username);
}

error log

ERROR: org.springframework.social.connect.web.ProviderSignInController - Exception while completing OAuth 2 connection: 
org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.

    at org.springframework.social.facebook.api.impl.AbstractFacebookOperations.requireAuthorization(AbstractFacebookOperations.java:30)
at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:48)
at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:42)
at org.springframework.social.facebook.connect.FacebookAdapter.setConnectionValues(FacebookAdapter.java:30)
at org.springframework.social.connect.support.AbstractConnection.setValues(AbstractConnection.java:175)
at org.springframework.social.connect.support.AbstractConnection.initKey(AbstractConnection.java:137)
at org.springframework.social.connect.support.OAuth2Connection.<init>(OAuth2Connection.java:75)

I do not know how to do this. Please help me.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ansatgol
  • 155
  • 4
  • 13

1 Answers1

1

This can be related to many causes but at first you should check your spring-social-facebook version. Since Facebook graph api 2.2 deprecation, Spring recommendation is to change the version to 2.0.3.RELEASE. Someone else was talking about a similar issue here "similar post

Don't try the 2.0.2.RELEASE, AFAIK there is an issue with inconsistent property type unmapped on json call.

Gael-Jurin
  • 36
  • 4
  • thanks for your information. Changing the version solved the issue. – ansatgol Apr 23 '17 at 15:26
  • Same problem. I then changed spring-social-facebook from version 1.1.1.RELEASE to version 2.0.3.RELEASE, and problem solved. Thanks. – Yuci May 26 '17 at 09:52