Evernote API Sandbox Config using Java Scribe

I had some difficulty in the beginning to setup Evernote using the Scribe library as by default the Scribe uses the Public API and not the Sandbox API that Evernote first gives you. You can then register you app with Evernote to get it Publicly available and then swap to the Public API class in Scribe.

package com.example.controller;

import static org.springframework.web.context.request.RequestAttributes.SCOPE_SESSION;

import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.example.oauth.OAuthServiceProvider;

import java.util.Map;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
 	
	@Autowired
	@Qualifier("evernoteServiceProvider")
	private OAuthServiceProvider evernoteServiceProvider;
	
    @RequestMapping(value="/login-evernote", method=RequestMethod.GET)
    public String loginToCloudSoleDevelop(Map<String, Object> map, WebRequest request) 
    {
    	// getting request and access token from session
		Token requestToken = (Token) request.getAttribute("oauthRequestToken", SCOPE_SESSION);
		Token accessToken = (Token) request.getAttribute("oauthAccessToken", SCOPE_SESSION);
		if(requestToken == null || accessToken == null) {
			// generate new request token
			OAuthService service = evernoteServiceProvider.getService();
			requestToken = service.getRequestToken();
			request.setAttribute("oauthRequestToken", requestToken, SCOPE_SESSION);
			
			// redirect to twitter auth page
			//service.getAuthorizationUrl(requestToken)
			System.out.println("RequestToken: " + requestToken.getToken());
			return "redirect:" + String.format("https://sandbox.evernote.com/OAuth.action?oauth_token=%s", requestToken.getToken());
		}
		return "login";
    }
    
    @RequestMapping(value={"/login"}, method = RequestMethod.GET)
	public ModelAndView callback(@RequestParam(value="oauth_token", required=false) String oauthToken,
			@RequestParam(value="oauth_verifier", required=false) String oauthVerifier, WebRequest request, Map<String, Object> map) {
		
		// getting request token
		OAuthService service = evernoteServiceProvider.getService();
		Token requestToken = (Token) request.getAttribute("oauthRequestToken", SCOPE_SESSION);
		
		// getting access token
		Verifier verifier = new Verifier(oauthVerifier);
		Token accessToken = service.getAccessToken(requestToken, verifier);
		
		// store access token as a session attribute
		request.setAttribute("oauthAccessToken", accessToken, SCOPE_SESSION);
		
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		HttpSession session = attr.getRequest().getSession(true); //create a new session
		session.setAttribute("accessToken",accessToken);
		
		ModelAndView mav = new ModelAndView("redirect:notes");
		return mav;
	}
}

Application Config

Note use the org.scribe.builder.api.EvernoteApi.Sandbox and not org.scribe.builder.api.EvernoteApi as this is the public Evernote API.

<bean id="evernoteServiceConfig" class="com.example.oauth.OAuthServiceConfig">
		<constructor-arg value="YOURTOKEN" />
		<constructor-arg value="YOURSECURITYKEY"/>
		<constructor-arg value="YOURURLREDIRECT"/>
		<constructor-arg value="org.scribe.builder.api.EvernoteApi.Sandbox"/>
	</bean>
<bean id="evernoteServiceProvider" class="com.example.oauth.OAuthServiceProvider">
		<constructor-arg name="config" ref="evernoteServiceConfig" />
</bean>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: