Created
October 29, 2014 00:33
-
-
Save taylorhughes/4dc50b2fe674c0a84cc6 to your computer and use it in GitHub Desktop.
Update Facebook token in iOS client from existing server-side cached token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[self.apiClient retrieveConnectedServicesWithSuccessBlock:^(NSDictionary *services) { | |
NSArray *facebookTokens = services[@"facebook"]; | |
if (facebookTokens.count == 0) { | |
return; | |
} | |
NSDictionary *mostRecentFacebookTokenDict = facebookTokens[0]; | |
NSNumber *refreshTimeNumber = mostRecentFacebookTokenDict[@"refreshTime"]; | |
NSDate *refreshDate = [NSDate dateWithTimeIntervalSince1970:[refreshTimeNumber doubleValue]]; | |
NSString *rawExpireDate = mostRecentFacebookTokenDict[@"expireTime"]; | |
NSDate *expireDate = [NSDate dateWithTimeIntervalSince1970:[rawExpireDate doubleValue]]; | |
NSNumber *loginTypeNumber = mostRecentFacebookTokenDict[@"loginType"]; | |
FBSessionLoginType loginType = [loginTypeNumber intValue]; | |
NSString *tokenString = mostRecentFacebookTokenDict[@"token"]; | |
NSArray *permissions = mostRecentFacebookTokenDict[@"permissions"]; | |
FBAccessTokenData *cachedTokenData = [FBAccessTokenData | |
createTokenFromString:tokenString | |
permissions:permissions | |
expirationDate:expireDate | |
loginType:loginType | |
refreshDate:refreshDate]; | |
NSString *facebookAppID = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookAppID"]; | |
NSString *urlSchemeSuffix = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookUrlSchemeSuffix"]; | |
FBSession *session = [[FBSession alloc] initWithAppID:facebookAppID | |
permissions:permissions | |
defaultAudience:FBSessionDefaultAudienceNone | |
urlSchemeSuffix:urlSchemeSuffix | |
tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance]]; | |
[FBSession setActiveSession:session]; | |
[session openFromAccessTokenData:cachedTokenData completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { | |
[self onFacebookSession:session stateChanged:status error:error]; | |
[self.apiClient addFacebookAccessToken:FBSession.activeSession.accessTokenData withSuccessBlock:^(NSDictionary *facebookTokenInfo) { | |
NSLog(@"Cached token data on server is old, added new facebook access token"); | |
} failureBlock:^(NSError *error) { | |
NSLog(@"Cached token data on server is old, but couldn't upload new facebook access token: %@", error); | |
}]; | |
}]; | |
} failureBlock:^(NSError *error) { | |
NSLog(@"Got error retrieving connected services: %@", error); | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment