[英]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.