且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Evernote iOS SDK - 如何使用令牌进行身份验证?

更新时间:2022-06-03 19:17:44

上周我遇到了同样的问题,他们的 SDK 确实不支持开箱即用,但经过一些研究我找到了解决方案完美无缺.此解决方案模拟了有效的身份验证流程.

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.

一点背景:

ENSession 类初始化时,它会检索保存在钥匙串上的凭据(除非之前调用了 [[ENSession sharedSession] unauthenticate]).问题是当使用不同的设备时钥匙串是空的,所以我们的目标是向 ENCredentialStore 添加一个有效的 ENCredentials 实例.

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 valid ENCredentials instance to the ENCredentialStore.

解决方案:

  1. 将以下导入添加到您的代码中:ENCredentials.hENCredentialStore.h.我们稍后会用到它们.

  1. Add the following imports to your code: ENCredentials.h and ENCredentialStore.h. We will need them later.

初始化 ENSession 就像你已经做的那样,使用 setSharedSessionConsumerKey:(NSString *)key consumerSecret:(NSString *)secret optionalHost:(NSString *)host.

Initialize the ENSession like you already do, using setSharedSessionConsumerKey:(NSString *)key consumerSecret:(NSString *)secret optionalHost:(NSString *)host.

为了创建一个有效的ENCredentials对象,我们需要提供以下对象:

In order to create a valid ENCredentials object, we need to provide the following objects:

  • NSString * 主机
  • NSString * edamUserId
  • NSString * noteStoreUrl
  • NSString * webApiUrlPrefix
  • NSString * authenticationToken
  • NSDate * expireDate
  • NSString * host
  • NSString * edamUserId
  • NSString * noteStoreUrl
  • NSString * webApiUrlPrefix
  • NSString * authenticationToken
  • NSDate * expirationDate

host 始终是 www.evernote.com(在 ENSessionBootstrapServerBaseURLStringUS 下的 ENSession 中定义).

The host is always www.evernote.com (as defined in ENSession under ENSessionBootstrapServerBaseURLStringUS).

edamUserId 是您在获得原始令牌时收到的用户 ID.expirationDate 也是如此.如果您不确定如何获取它们,那么您应该在通过身份验证后使用 [[ENSession sharedSession].userStore getUserWithSuccess:^(EDAMUser *user).

edamUserId is the user id you received when you got the original token. Same for the expirationDate. If you are not sure how to get them then you should use [[ENSession sharedSession].userStore getUserWithSuccess:^(EDAMUser *user) once authenticated.

因此,实际上唯一缺少的对象是 noteStoreUrlwebApiUrlPrefix.它们的格式始终为:

So the only objects that are actually missing are noteStoreUrl and webApiUrlPrefix. Their format is always:

  • noteStoreUrl:https://www.evernote.com/shard/edam_shard/notestore
  • webApiUrlPrefix:https://www.evernote.com/shard/edam_shard/

幸运的是,您的令牌已经包含 edam_shared(S= 的值,请参阅 这个):

Luckily, your token already contains edam_shared (value of S=, see this):

@"S=s161:U=5ce3f20:E=1561182201b:C=24eb9d000f8:P=285:A=app:V=2:H=e8ebf56eac26aaacdef2f3caed0bc309"

@"S=s161:U=5ce3f20:E=1561182201b:C=24eb9d000f8:P=285:A=app:V=2:H=e8ebf56eac26aaacdef2f3caed0bc309"

如果您提取 s161 并将其放在上面的 URL 中,它将起作用(我确定您知道如何提取它,但如果您遇到问题,请告诉我).

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

现在我们已准备好使用令牌进行身份验证.首先,使用类别从 ENSession 公开必要的功能:

Now we are ready to authenticate using the token. First, expose the necessary functions from ENSession using a category:

@interface ENSession(Authentication)

- (void)startup;

- (void)addCredentials:(ENCredentials *)credentials;

@end

并使用令牌进行身份验证:

And authenticate using the token:

ENCredentials *credentials = [[ENCredentials alloc] initWithHost:ENSessionBootstrapServerBaseURLStringUS edamUserId:userId noteStoreUrl:noteStoreUrl webApiUrlPrefix:webApiUrlPrefix authenticationToken:token expirationDate:expirationDate];
[[ENSession sharedSession] addCredentials:credentials];
[[ENSession sharedSession] startup];

为了刷新 ENSession 并检索新存储的凭据,最后一行很重要.

The last line is important in order to refresh the ENSession and retrieve the new stored credentials.

现在您已通过身份验证并准备好查询 SDK.祝你好运.

Now you are authenticated and ready to query the SDK. Good luck.