[英]Yahoo authenticate & fetch profile details iOS
我在这里列出了我遵循的Yahoo Integration步骤 。
fno-objc-arc
。 #import "YOSSocial.h"
。 oauth_token
和oauth_verifier
。 代码块1
- (void)viewDidLoad {
[super viewDidLoad];
self.session = [YOSSession sessionWithConsumerKey:@"ConsumerKeyHere"
andConsumerSecret:@"ConsumerSecretKeyHere"
andApplicationId:@"AppKey"];
BOOL hasSession = [self.session resumeSession];
if(hasSession == FALSE) {
// custom call back URL which will redirect to our-app.
// 10.0.0.76/iOS/callback.php redirects
// to com.mymobileapps.currentApp.yahoo
[self.session
sendUserToAuthorizationWithCallbackUrl:
@"http://10.0.0.76/iOS/callback.php"];
} else {
[self sendRequests];
}
}
代码块2
- (void)sendRequests {
// initialize a user request for the logged-in user
YOSUserRequest *request = [YOSUserRequest requestWithSession:self.session];
// fetch the user's profile data
[request fetchProfileWithDelegate:self];
}
- (void)requestDidFinishLoading:(YOSResponseData *)data {
// parse the response text string into a dictionary
NSDictionary *rspData = [NSJSONSerialization JSONObjectWithData:[data.responseText dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSDictionary *profileData = [rspData objectForKey:@"profile"];
// format a string using the nickname object from the profile.
NSString *welcomeText = [NSString stringWithFormat:@"Hey %@ %@!",
[profileData objectForKey:@"givenName"],
[profileData objectForKey:@"familyName"]];
NSLog(@"welcometext is %@",welcomeText);
self.lblProfile.text = welcomeText;
}
代码块3
- (BOOL)application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
NSString *str = [[url description] stringByReplacingOccurrencesOfString:@"com.mymobileapps.currentApp.yahoo://oauth-response?oauth_token=" withString:@""];
NSArray *ar = [str componentsSeparatedByString:@"&oauth_verifier="];
NSLog(@"oauth_token is %@",[ar objectAtIndex:0]);
NSLog(@"oauth_verifier is %@",[ar objectAtIndex:1]);
// How my session will get updated now with valid authentication done?
return YES;
}
我遵循了这里描述的每一步 - http://developer.yahoo.com/social/sdk/objectivec/我还实现了重定向,如此处所述 - 如何在身份验证后从Yahoo重定向到我的IOS应用程序?
问题如下。 我仍然无法获取性别,出生日期等用户个人资料详细信息。这是 - 从代码块2 ,我收到的数据为零。
我的代码中缺少什么来检索用户配置文件的数据?
其他参考。
- (BOOL)application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
上面的代码说明了Google+框架如何处理重定向并使用本地会话进行管理。 在雅虎的情况下,我找不到任何有助于更新移动应用程序的本地会话的细节。
编辑:
如果通过Yahoo OAuth无法实现, 如何从雅虎获取基本的个人资料详细信息(如性别,出生日期,电子邮件ID,姓名等)?
这是解决方案。
注意:您将需要一个中间服务器。
http://10.0.0.76/iOS/yos-social-php-master/sample/sampleapp.php
URL types
, URL identifier
和URL Schemes
。 如代码块3所示 yourLocalServer/iOS/yos-social-php-master/sample/sampleapp.php
文件。 ( https://github.com/yahoo/yos-social-php/blob/master/sample/sampleapp.php ) 摘要
移动应用程序导航到服务器 - >服务器通过OAuth-php管理身份验证。 经过身份验证的服务器检索配置文件详细信息,服务器指示safari导航回 - your-mobile-App。 您的移动应用程序获取Code Block中的所有详细信息
代码块1
- (IBAction)connectYahoo:(id)sender {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:
@"http://yourLocalServer/iOS/yos-social-php-master/sample/sampleapp.php"
]];
}
代码块2
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if([[url scheme] isEqualToString:@"com.yourcompany.app.fsq"]) {
return [self.obj_LoginHomeVCtr.foursquare handleOpenURL:url];
} else if([[url scheme] isEqualToString:@"com.yourcompany.app.googleplus"]){
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}else if([[url scheme] isEqualToString:@"fb188315544652080"]){
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(@"In fallback handler");
}];
} else if ([[url scheme] isEqualToString:@"com.yourcompany.app.yahoo"]){
STLog(@"URL is %@",url);
return YES;
}
return YES;
}
代码块3
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.yourcompany.app.yahoo</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.yourcompany.app.yahoo</string>
</array>
</dict>
</array>
代码块4
else if($hasSession && $profile) {
$string = "com.yourcompany.app.yahoo://response?birthdate=" . $profile->birthdate . "&familyName=" . $profile->familyName. " " . $profile->givenName . "&gender=" . $profile->gender . "&guid=" . $profile->guid . "&image=" . $profile->image->imageUrl;
echo '<meta http-equiv="Refresh" content="1;URL='. $string .'">';
}
?>
fetchProfileWithDelegate:
source here )构建一个URL并设置一些头信息,然后将这些数据传递给[YOSRequestClient -sendAsyncRequestWithDelegate:]
( 这里是源码 )。
然后,请求客户端创建NSMutableURLRequest
和NSURLConnection
并启动连接。
下载数据后(如果有的话), YOSResponseData
接管并从下载的数据中创建一个新对象( 此处为源代码 )。
没有我能看到的代码路径允许传入的serviceResponseData
对象为nil
。 您至少应该能够看到[data didSucceed]
,它会告诉您HTTP响应是否< 400
。 奇怪的是,如果服务器只是打开并关闭没有HTTP响应的连接,我相信[data didSucceed] == YES
即使它显然没有成功(因为0 <400)。
看起来你做错了什么。 我的猜测是,自从上次提交是4年以来 ,该公司从那时起经历了重大的重组,该项目已被放弃,没有人打扰记录它。
更新:除了4年没有更新,雅虎的该软件开发者论坛已经关闭 。 我不认为雅虎的这个软件的API正在运行。 -
在这里链接的雅虎文档中有几点要检查:
检查您是否正在请求并获得用户的正确权限的批准
检查YOSResponseData
对象,如果发生错误,它应包含NSError
对象。
它还包含NSHTTPURLResponse
对象。 检查响应标头和状态代码。
您可能已经检查了这些内容,如果是这样,请将结果添加到问题中。
您现在可以使用更新的Yahoo iOS SDK而无需执行如此多的步骤: https : //github.com/yahoo/yos-social-objc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.