授权Twitter成功后转回应用程序 [英] Turn back to app after authorize Twitter Successful
问题描述
我将我的应用配置为使用登录Twitter 功能。但在Safari上授权应用程序成功后,我无法回到我的应用程序。我在堆栈溢出时看到了同样问题的另一个问题
原始帖子
您需要确保您的oauthCallback是列出的方案在您的Apps plist中。您还可以使用更清晰的反向域主机方案,以确保您永远不会与任何其他应用程序冲突。
因此您的App plist看起来像这样:
我使用 com.twitteroauthcallback.sutrixsnowball 作为网址方案。
你需要处理您的应用代理中的此回调。这样的事情:
- (BOOL)应用程序:(UIApplication *)应用程序openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication注释:(id)注释{
if([[[url scheme] lowercaseString] isEqualToString:@com.twitteroauthcallback.sutrixsnowball] ==否){
返回NO;
}
NSMutableDictionary * twitterParamsDictionary = [NSMutableDictionary dictionary];
NSArray * queryComponents = [[url query] componentsSeparatedByString:@&];
for(NSCtring * s在queryComponents中){
NSArray * pair = [s componentsSeparatedByString:@=];
if([pair count]!= 2)继续;
NSString * key = pair [0];
NSString * value = pair [1];
twitterParamsDictionary [key] = value;
}
NSString * token = twitterParamsDictionary [@oauth_token];
NSString * verifier = twitterParamsDictionary [@oauth_verifier];
if(token&& verifier){
[self setOAuthToken:token oauthVerifier:verifier];
返回YES;
}
返回NO;
}
- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {
__weak typeof(self)weakself = self;
[self.twitterAPI postAccessTokenRequestWithPIN:verifier
successBlock:^(NSString * oauthToken,NSString * oauthTokenSecret,NSString * userID,NSString * screenName){
[weakself.twitterAPI getUserInformationFor:screenName
successBlock:^(NSDictionary * user){
///我们有一个帐户
} errorBlock:^(NSError * error){
/// error
}];
} errorBlock:^(NSError * error){
/// error
}];
}
你会注意到 postAccessTokenRequestWithPin的成功阻止:
方便地传回令牌和秘密。查看STTwitterAPI,您将看到以下构造函数:
+(instancetype)twitterAPIWithOAuthConsumerKey:(NSString *)consumerKey
consumerSecret :(NSString *)consumerSecret
oauthToken:(NSString *)oauthToken
oauthTokenSecret:(NSString *)oauthTokenSecret
因此,如果您将这些值保留在设备的某个位置,您可以在下次启动时登录Twitter:
///下一个应用程序启动
STTwitterAPI * twitterAPI = [STTwitterAPI twitterAPIWithOAuthConsumerKey:consumerKey consumerSecret:consumerSecret oauthToken:oauthToken oauthTokenSecret:oauthTokenSecret];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString * username){
///我们仍然很高兴去
} errorBlock:^(NSError * error){
/// token已经过期。用户需要再次登录
}];
编辑
事实证明比为了工作STTwitter要求oauthCallback看起来像这样
oauthCallback:@< YOUR_URL_SCHEME>:// twitter_access_tokens /
I config my app to use the login by twitter feature. But I cannot turn back to my app after authorize app success on safari. I saw another question with the same problem on stack overflow use UrlShemes but this not work with me(I don't have enough reputation to add comments to ask them into the accept answer).So please let me know: If I can turn back to my app after authorize successful (like Facebook API can do)? If this is possible, so how to do this?
Here is my code:
self.twitter = [STTwitterAPI twitterAPIWithOAuthConsumerKey:Twitter_API_KEY
consumerSecret:Twitter_API_SECRET];
[_twitter postTokenRequest:^(NSURL *url, NSString *oauthToken) {
NSLog(@"-- url: %@", url);
NSLog(@"-- oauthToken: %@", oauthToken);
[[UIApplication sharedApplication] openURL:url];
} authenticateInsteadOfAuthorize:NO
forceLogin:@(YES)
screenName:nil
oauthCallback:@"sutrixsnowball://"
errorBlock:^(NSError *error) {
NSLog(@"-- error: %@", error);
}];// The "sutrixsnowball" is URL Schemes item into plist file
It just open the web page on safari like the image below (It's not a real url when I copy, so I post the image):
Note For those still using STTwitter I would recommend switching over to Twitter's official SDK found here
Original Post
You need to make sure that your oauthCallback is a scheme listed in your Apps plist. You could also use a cleaner reverse domain host scheme just to be super sure you never conflict with any other Apps.
So your App plist would look something like this:
Where I'm using com.twitteroauthcallback.sutrixsnowball as the URL scheme.
The you need to handle this callback in your app delegate. Something like this:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([[[url scheme] lowercaseString] isEqualToString:@"com.twitteroauthcallback.sutrixsnowball"] == NO) {
return NO;
}
NSMutableDictionary *twitterParamsDictionary = [NSMutableDictionary dictionary];
NSArray *queryComponents = [[url query] componentsSeparatedByString:@"&"];
for(NSString *s in queryComponents) {
NSArray *pair = [s componentsSeparatedByString:@"="];
if([pair count] != 2) continue;
NSString *key = pair[0];
NSString *value = pair[1];
twitterParamsDictionary[key] = value;
}
NSString *token = twitterParamsDictionary[@"oauth_token"];
NSString *verifier = twitterParamsDictionary[@"oauth_verifier"];
if (token && verifier) {
[self setOAuthToken:token oauthVerifier:verifier];
return YES;
}
return NO;
}
- (void)setOAuthToken:(NSString *)token oauthVerifier:(NSString *)verifier {
__weak typeof (self)weakself = self;
[self.twitterAPI postAccessTokenRequestWithPIN:verifier
successBlock:^(NSString *oauthToken, NSString *oauthTokenSecret, NSString *userID, NSString *screenName) {
[weakself.twitterAPI getUserInformationFor:screenName
successBlock:^(NSDictionary *user) {
///we have an account
} errorBlock:^(NSError *error) {
/// error
}];
} errorBlock:^(NSError *error) {
///error
}];
}
You will notice that the success block for the postAccessTokenRequestWithPin:
conveniently passes back a token and a secret. Looking at STTwitterAPI you will see the following constructor:
+ (instancetype)twitterAPIWithOAuthConsumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
oauthToken:(NSString *)oauthToken
oauthTokenSecret:(NSString *)oauthTokenSecret
So if you keep those values somewhere on the device you can login to twitter on next startup:
/// Next app launch
STTwitterAPI *twitterAPI = [STTwitterAPI twitterAPIWithOAuthConsumerKey:consumerKey consumerSecret:consumerSecret oauthToken:oauthToken oauthTokenSecret:oauthTokenSecret];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
/// we still good to go
} errorBlock:^(NSError *error) {
/// token has expired. User needs to login again
}];
EDIT
It turns out than in order to work STTwitter requires that the oauthCallback look something like this
oauthCallback:@"<YOUR_URL_SCHEME>://twitter_access_tokens/"
这篇关于授权Twitter成功后转回应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!