从Twitter API获取当前用户的信息
我正在构build一个Twitter应用程序(使用oAuth),我无法find任何有关如何获取当前用户信息的信息。
当前用户,我的意思是授予我的应用程序权限的用户和应用程序向API发出请求的用户。
有什么build议么?
它实际上是“account / verify_credentials”,如下所示:
https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
链接到API 1.1版的文档: https : //dev.twitter.com/docs/api/1.1/get/account/verify_credentials
使用oAuth,你可以打电话从你的TwitterOAuth对象获取用户信息。
例如,如果
$oauth = new TwitterOAuth([keys here]); $credentials = $oauth->get('account/verify_credentials'); echo "Connected as @" . $credentials->screen_name ."\n";
解决scheme尊重iOS:
1)如果你的设备上有一个Twitter帐号(例如ACAccount
的ACAccountStore
),你只需要把它附加到SLRequest上,并从返回的字典中获取所有的用户信息:
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/verify_credentials.json"]; NSMutableDictionary *params = [NSMutableDictionary new]; [params setObject:[Twitter sharedInstance].session.userID forKey:@"user_id"]; [params setObject:@"0" forKey:@"include_entities"]; [params setObject:@"1" forKey:@"skip_status"]; [params setObject:@"1" forKey:@"include_email"]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params]; [request setAccount:twitterAccount]; //one of the Twitter Acc [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (responseData) { NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; }); }else { NSLog(@"Error while downloading Twitter user data: %@", error); } }];
2)否则 – 您需要发送OAuth(X-Auth)用户证书。 最简单的方法是使用TWTROAuthSigning类来检索OAuth-params:
TWTROAuthSigning *oauthSigning = [[TWTROAuthSigning alloc] initWithAuthConfig: [Twitter sharedInstance].authConfig authSession:session]; NSDictionary *authHeaders = [oauthSigning OAuthEchoHeadersToVerifyCredentials];
并发送普通的请求附加凭证作为头字段:
NSString *oauthParameters = authHeaders[@"X-Verify-Credentials-Authorization"]; NSString *urlString = authHeaders[@"X-Auth-Service-Provider"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; request.allHTTPHeaderFields = @{@"Authorization":oauthParameters}; [request setHTTPMethod:@"GET"]; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){ NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL]; }];
如果您支持旧版本的Twitter SDK或search更多选项,我build议您查看STTwitter库