0

I am New to Spring ,and I want to log into a Spring MVC app. With LinkedIn , I use SCRIBE for login , but when I run the application, it throws an exception.

My controller code for linkedin in MVC is like:

  @RequestMapping(value="/loginlinkedin", method=RequestMethod.GET)
    public String callback(WebRequest request){

        OAuthService service=linkedInServiceProvider.getService();
        System.out.println("above request token"); 
        //request token
        Token requestToken=service.getRequestToken(); 

        System.out.println(requestToken);
        System.out.println("below request token");

        //getting authorized url
        String oauth_verif= service.getAuthorizationUrl(requestToken);
        System.out.println(oauth_verif);

        //verifing the requested token
        Verifier verifier=new Verifier(in.nextLine());
        System.out.println("below verifier"+verifier);

        //get AccessToken
        Token accessToken=service.getAccessToken(requestToken, verifier);
        System.out.println("access tolen"+accessToken);


        OAuthRequest oauthRequest=new OAuthRequest(Verb.GET, "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,headline)");
        System.out.println("outhrequest"+oauthRequest);
        service.signRequest(accessToken, oauthRequest);
        Response outhResponse=oauthRequest.send();
        System.out.println(outhResponse.getBody());
        //ModelAndView mav=new ModelAndView("redirect:loginPage");
        return "result";
    }

and beans for linked login:

<bean id="linkedInServiceProvider" class="com.spring.handler.OAuthServiceProvider">
    <constructor-arg name="oauthServiceConfig" ref="linkedInServiceConfig" />
</bean

And OAuthServiceConfig class for configuration of service:

  package com.spring.handler;
import org.scribe.builder.api.Api;
public class OAuthServiceConfig {
private String apiKey;
private String apiSecret;
private String callback;
private Class apiClass;

public OAuthServiceConfig(String apiKey,String apiSecret,String callback,Class apiClass){
    super();
    this.apiKey=apiKey;
    this.apiSecret=apiSecret;
    this.callback=callback;
    this.apiClass=apiClass;
    System.out.println(apiKey+apiSecret+callback+apiClass);
}

public String getApiKey() {
    return apiKey;
}

public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}

public String getApiSecret() {
    return apiSecret;
}

public void setApiSecret(String apiSecret) {
    this.apiSecret = apiSecret;
}

public String getCallback() {
    return callback;
}

public void setCallback(String callback) {
    this.callback = callback;
}

public Class getApiClass() {
    return apiClass;
}

public void setApiClass(Class apiClass) {
    this.apiClass = apiClass;
}



}

and OAuthServiceProvider class for providing the service to OAuthService using linkedInServiceProvider.getService() method:

   package com.spring.handler;

import org.scribe.builder.ServiceBuilder;
import org.scribe.oauth.OAuthService;

public class OAuthServiceProvider {
    OAuthServiceConfig oauthServiceConfig;

    public OAuthServiceProvider() {
    }

    public OAuthServiceProvider(OAuthServiceConfig oauthServiceConfig) {
        this.oauthServiceConfig = oauthServiceConfig;
    }

    public OAuthService getService() {
        System.out.println(oauthServiceConfig);
        return new ServiceBuilder().provider(oauthServiceConfig.getApiClass())
                .apiKey(oauthServiceConfig.getApiKey())
                .apiSecret(oauthServiceConfig.getApiSecret())
                .callback(oauthServiceConfig.getCallback()).build();
    }
}

But when I log into the application, the exception thrown is:

May 28, 2015 5:42:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [HelloWebHandler] in context with path [/SpringHandler] threw exception [Request processing failed; nested exception is org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=permission_unknown'] with root cause
org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=permission_unknown'
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:104)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:85)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:90)
    at com.spring.handler.UserController.callback(UserController.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
Sunoo
  • 1
  • 6
  • Possible duplication of http://stackoverflow.com/questions/29755678/apache-oltu-linkedin-integration-example – Ranjitsinh May 28 '15 at 14:50

1 Answers1

0

permission_unknown: The User hasn't decided whether to permit this Consumer to access Protected Resources. Usually happens when the Consumer requests Access Token before the user completes authorization process.

To trace and solve your problem :

  • Turn the scribe debug mode on executing debug() method on ServiceBuilder.
  • Read carefully the Getting started guide from scribe-java
  • Review your code for inconsistences:
    //verifing the requested token
    Verifier verifier=new Verifier(in.nextLine());
    System.out.println("below verifier"+verifier);

What's in? you have to verify the request token, seems like you copied that line from the scribe examples, anyway it's hard to believe that your code works because in is not declared.

  • Rebuild your controller: implement the start authentication method first, get or store the request token attribute and redirect to the authorization url (linkedin url) if its required (service.getAuthorizationUrl()); implement your callback method, (after the user has approved the access to his profile, he is redirected to our callback endpoint defined in your OAuthServiceConfig class) and get the oauth_verifier and the request token to verify and obtain the accessToken, store it, sign the request... etc... etc
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
  • no way , i didn't copy anywhere ,yeah i follow the same guide as you link above ,Actually i forget to write Scanner in=new Scanner(System.in) at here ; and when it reached at Verifier verifier=new Verifier(in.nextLine()); than , i give the secret key of LinkedIn Application as parameter and after its return the verifier object.after that ,,problem .. to access ,token. – Sunoo May 28 '15 at 17:15
  • in suggested guide , , what is "verifier you got from the user" in Verifier v = new Verifier("verifier you got from the user"); – Sunoo May 28 '15 at 17:17
  • Is the oauth_verifier passed to your callback method after the linkedin authentication. – vzamanillo May 28 '15 at 19:05
  • No ,I debug my code than code is running okay till verifier after that, im getting the problem in access the tokens,Exception is "Response Body Incorrect, Cann't Extract token and Secret" – Sunoo May 29 '15 at 02:03
  • You did not understand how scribe works and you did not understand how oauth works and you did not rebuild your controller,anyway your app will not work because you are not storing nothing in the session attributes, sorry I can't help you anymore. – vzamanillo May 29 '15 at 08:42
  • No , I am working on it, and i understand how scribe works , now i am rebuild the controller ,, so , please , give me some time to rebuild the controller... – Sunoo May 29 '15 at 09:20