Evernote iOS SDK - 如何使用令牌进行身份验证? [英] Evernote iOS SDK - How do I authenticate with a token?

查看:58
本文介绍了Evernote iOS SDK - 如何使用令牌进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 iOS 版 Evernote SDK,当用户获得授权访问权限时,我正在保存身份验证令牌.

I am using Evernote SDK for iOS and I am saving the authentication token when the user has authorized access.

一旦用户在其他设备上安装我的应用程序,我想使用该令牌自动重新进行身份验证,但 SDK 似乎不支持该功能.有没有办法做到这一点?

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?

推荐答案

上周我遇到了同样的问题,他们的 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.

这篇关于Evernote iOS SDK - 如何使用令牌进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆