I am using Evernote SDK for iOS and I am saving the authentication token when the user has authorized access.
Once the user installs my application on a different device, I want to use that token to reauthenticate automatically, but it looks like SDK doesn't support that. Is there a way to do that?
I had the same issue last week, and their SDK indeed doesn't support it out-of-the-box, but after some research I found a solution that works perfectly. This solution mimics a valid authentication flow.
A little background:
When the
ENSession
class initializes, it retrieves the credentials that are saved on the keychain (unless[[ENSession sharedSession] unauthenticate]
was called earlier). The problem is that the keychain is empty when using a different device, so our goal is to add a validENCredentials
instance to theENCredentialStore
.Solution:
Add the following imports to your code:
ENCredentials.h
andENCredentialStore.h
. We will need them later.Initialize the
ENSession
like you already do, usingsetSharedSessionConsumerKey:(NSString *)key consumerSecret:(NSString *)secret optionalHost:(NSString *)host
.In order to create a valid
ENCredentials
object, we need to provide the following objects:NSString *
hostNSString *
edamUserIdNSString *
noteStoreUrlNSString *
webApiUrlPrefixNSString *
authenticationTokenNSDate *
expirationDateThe
host
is alwayswww.evernote.com
(as defined inENSession
underENSessionBootstrapServerBaseURLStringUS
).edamUserId
is the user id you received when you got the original token. Same for theexpirationDate
. If you are not sure how to get them then you should use[[ENSession sharedSession].userStore getUserWithSuccess:^(EDAMUser *user)
once authenticated.So the only objects that are actually missing are
noteStoreUrl
andwebApiUrlPrefix
. Their format is always:https://www.evernote.com/shard/edam_shard/notestore
https://www.evernote.com/shard/edam_shard/
Luckily, your token already contains
edam_shared
(value ofS=
, see this):If you extract
s161
and put it in the URLs above it will work (I am sure you know how to extract that, but let me know if you're having problems).Now we are ready to authenticate using the token. First, expose the necessary functions from ENSession using a category:
And authenticate using the token:
The last line is important in order to refresh the
ENSession
and retrieve the new stored credentials.Now you are authenticated and ready to query the SDK. Good luck.