Twitter集成在Iphone App中

时间:2010-12-17 06:47:06

标签: iphone cocoa-touch twitter

我使用了MGTwitterEngine

尝试登录然后请求失败错误

错误明细

connectionIdentifier 6260FAFD-AE4E-4F05-AE67-FFA1DA18578F

错误说明无法完成操作。 (HTTP错误401.)

错误用户信息 ({ body =“\ n \ n客户端应用程序不允许使用xAuth。\ n / oauth / access_token \ n \ n”; response =“”; })

任何想法

-Amit Battan

4 个答案:

答案 0 :(得分:3)

Twitter不再支持基本身份验证。您可以使用Twitter-oAuth-iPhone。它添加了oAuth和MGTwitterEngine。还包含一个演示代码项目。但是,您需要SDK> = 3.2才能编译最新版本。

xAuth需要向Twitter API团队请求提供应用程序详细信息的问题。如果是oAuth,则不需要。您需要在Twitter Dev中创建一个应用程序,然后您需要使用项目中的键。就这样。

答案 1 :(得分:1)

MGTwitterEngine此时显着过时,因为它不使用xAuth / oAuth。请参阅本教程,了解如何将MGTwitterEngine与xAuth一起使用:

<强> Switching from Basic to xAuth with mgtwitterengine on iPhone

答案 2 :(得分:1)

OAuth不需要Twitter的许可。但XAuth是必需的。

我没有使用 MGTwitterEngine ,使用Twitter的官方文档,我用C抓住客户端来计算nonce和签名,然后使用工具cURL,然后我就可以得到oauth_token和token_secret。

有关cURL的其他消息是您必须在Twitter中创建了应用程序,您可以使用应用程序中的OAuth tool(不需要是您正在处理的当前应用程序)设置。如果twitter中的应用程序不是您当前的应用程序,则应首先生成cURL命令行。将请求设置设置为POST,将请求URI 设置为http://api.twitter.com/oauth/request_token,将其他设置保留为默认设置,然后点击页面底部的按钮,然后你看到cURL命令,看起来像是:

curl --request 'POST' 'http://api.twitter.com/oauth/request_token' --header 'Authorization: OAuth oauth_consumer_key="your_consumer_key", oauth_nonce="your_random_string", oauth_signature="your_signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1329273306", oauth_token="451145411-AxGXG8xNdW1Eymwx85hs3sc1OE3NYsXe578AUtPe", oauth_version="1.0"' --verbose

oauth_consumer_keyoauth_timestampoauth_signature替换为您的,删除oauth_token及其值,将命令复制到cmd或{{1并执行它。另一个重要的诀窍是,您必须在shell中明确标记oauth_callback,并将值设置为Authorization

oob

如果您的auth_callback="oob" consumer_key正确无误,则此步骤不会导致oauth_signature错误。

答案 3 :(得分:0)

@taskinoor 那里有我正在使用的代码...... 我已经在twitter上创建了应用程序

控制器.h文件

#import <UIKit/UIKit.h>
#import "MGTwitterEngine.h"
@class OAToken;
@interface TwitterTestViewController : UIViewController <MGTwitterEngineDelegate> { 
    MGTwitterEngine *twitterEngine;
    OAToken *token;
}
@end

控制器.m文件

#import "TwitterTestViewController.h"
@implementation TwitterTestViewController
- (void)awakeFromNib{
    // Put your Twitter username and password here:
    NSString *username = @"amit_bursys";
    NSString *password = @"mypassword";
    NSString *consumerKey = @"my_key";
    NSString *consumerSecret = @"my_seret";
    if (! username || ! password || !consumerKey || !consumerSecret) {
        NSLog(@"You forgot to specify your username/password/key/secret in AppController.m, things might not work!");
        NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
    }
    twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
    [twitterEngine setUsesSecureConnection:NO];
    [twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
    [twitterEngine setUsername:username];
    [twitterEngine getXAuthAccessTokenForUsername:username password:password];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
    [super dealloc];
}
#pragma mark MGTwitterEngineDelegate methods
- (void)requestSucceeded:(NSString *)connectionIdentifier{
    NSLog(@"Request succeeded for connectionIdentifier = %@", connectionIdentifier);
}
- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error{
    NSLog(@"Request failed for connectionIdentifier = %@, error = %@ (%@)", 
          connectionIdentifier, 
          [error localizedDescription], 
          [error userInfo]);
}
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier{
    NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);
}
- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier{
    NSLog(@"Got direct messages for %@:\r%@", connectionIdentifier, messages);
}
- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
{
    NSLog(@"Got user info for %@:\r%@", connectionIdentifier, userInfo);
}
@end

,代码日志

2010-12-21 11:14:31.533 TwitterTest[734:207] Request failed for connectionIdentifier = D28BD476-7315-4510-B51A-968E516D169D, error = The operation couldn’t be completed. (HTTP error 401.) ({
    body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <request>/oauth/access_token</request>\n  <error>Client application is not permitted to use xAuth.</error>\n</hash>\n";
    response = "<NSHTTPURLResponse: 0x6022470>";
})