【发布时间】:2014-02-24 18:16:20
【问题描述】:
我正在将 twitter 集成到我的应用程序中。我用谷歌搜索并在堆栈中搜索,找不到正确的步骤。 谁能详细告诉我它是如何工作的。 谢谢。
【问题讨论】:
我正在将 twitter 集成到我的应用程序中。我用谷歌搜索并在堆栈中搜索,找不到正确的步骤。 谁能详细告诉我它是如何工作的。 谢谢。
【问题讨论】:
我希望这会对你有所帮助。 . .
1.将以下类添加到您的项目中
GTMOAuthAuthentication.h/m
GTMOAuthSignIn.h/m
GTMHTTPFetcher.h/m
GTMOAuthViewControllerTouch.h/m
GTMOAuthViewTouch.xib
2 。添加以下框架 Security.framework 和 SystemConfiguration.framework。
3 .set -ObjC 为应用程序目标的“其他链接器标志”构建选项。
4 。然后是时候进行一些编码了。
import GTMOAuthAuthentication.h and GTMOAuthViewControllerTouch.h
- (void)signInWithTwitter
{
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
NSString *scope = @"http://api.twitter.com/";
GTMOAuthAuthentication *auth = [self authForTwitter];
[auth setCallback:@"http://www.noop.com/OAuthCallback"];
GTMOAuthViewControllerTouch *viewController;
viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
language:nil
requestTokenURL:requestURL
authorizeTokenURL:authorizeURL
accessTokenURL:accessURL
authentication:auth
appServiceName:@"AppName : Twitter"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[appDelegate.navigationController pushViewController:viewController animated:YES];
}
- (GTMOAuthAuthentication *)authForTwitter {
GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
consumerKey:TWITTER_CONSUMER_KEY
privateKey:TWITTER_CONSUMER_SECRET];
[auth setServiceProvider:@"Twitter"];
return auth;
}
- (void)viewController:(GTMOAuthViewControllerTouch *)viewController finishedWithAuth:(GTMOAuthAuthentication *)auth error:(NSError *)error {
if(error)
{
//handle error
}
else
{
// do stuff as per your app.
}
}
NOte : if you get error message like "failed to validate oauth signature and token" then check you system time is correct or not .
【讨论】:
1) 下载 oauth 库,用于 ios 的身份验证。 2) 使用所有密钥在 Twitter 上创建帐户 消费者密钥,消费者秘密,访问密钥访问令牌 将 int 添加到 .... 3) -(void)getList{
OAConsumer *con = [[OAConsumer alloc]initWithKey:@"TwitterConsumerKey" secret:@"TwitterConsumerSecret"];
OAToken *p=[[OAToken alloc]initWithKey:[@"TwitterAccessTokenKey" secret:@"TwitterAccessTokenSecret"];
int kPageCount=10; NSString* pro=[NSString stringWithFormat:@"%@count=%i", @"https://api.twitter.com/1.1/statuses/user_timeline.json?",kPageCount];
NSURL *tt=[NSURL URLWithString:pro];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:tt
consumer:con
token:p
realm:nil
signatureProvider:nil] ;
[request setHTTPMethod:@"GET"];
fetcher = [[OADataFetcher alloc] init] ;
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(requestTokenTicket:didFinishWithData:)
didFailSelector:@selector(requestTokenTicket:didFailWithError:)];
}
}
- (void)requestTokenTicket:(OAServiceTicket *)ticket didFailWithError:(NSError *)err {
if (err.domain==NSURLErrorDomain && err.code==-1001) {
[self getList];
}
} - (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responseBody :%@",responseBody);
}
【讨论】: