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

时间:2015-07-23 08:31:42

标签: ios oauth evernote

我正在使用Evernote SDK for iOS,并且当用户授权访问时我保存了身份验证令牌。

一旦用户在不同的设备上安装我的应用程序,我想使用该令牌自动重新进行身份验证,但看起来SDK不支持。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:8)

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

一点背景:

ENSession类初始化时,它会检索保存在钥匙串上的凭据(除非先前调用[[ENSession sharedSession] unauthenticate])。问题是当使用其他设备时,钥匙串是空的,因此我们的目标是向ENCredentials添加有效的ENCredentialStore实例。

<强>解决方案:

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

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

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

    • NSString *主持人
    • NSString * edamUserId
    • NSString * noteStoreUrl
    • NSString * webApiUrlPrefix
    • NSString * authenticationToken
    • NSDate * expirationDate

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

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

    因此,实际缺失的唯一对象是noteStoreUrlwebApiUrlPrefix。他们的格式总是:

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

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

      

    @&#34; S = S161:U = 5ce3f20:E = 1561182201b:C = 24eb9d000f8:P = 285:A =应用程式:V = 2:H = e8ebf56eac26aaacdef2f3caed0bc309&#34;

    如果你提取s161并将其放在上面的网址中,它就会有用(我相信你知道如何提取它,但如果你遇到问题请告诉我。)

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

    @interface ENSession(Authentication)
    
    - (void)startup;
    
    - (void)addCredentials:(ENCredentials *)credentials;
    
    @end
    

    使用令牌进行身份验证:

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

    最后一行非常重要,可以刷新ENSession并检索新存储的凭据。

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