适用于iOS的MGTwitterEngine:如何获取accessToken?错误401

时间:2012-03-15 11:15:18

标签: ios authentication twitter access-token mgtwitterengine

我正在使用MGTwitterEngine库为我的iOS应用发布推文。

NSString *username = dataBase.twiLog;
NSString *password = dataBase.twiPas;                    

NSString *consumerKey = cons_key;
NSString *consumerSecret = cons_secret;

// Most API calls require a name and password to be set...
    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.");
 }       
// Create a TwitterEngine and set our login details.
MGTwitterEngine *twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
twitterEngine setClearsCookies:YES];                        
[twitterEngine setUsesSecureConnection:NO];
[twitterEngine setConsumerKey:consumerKey secret:consumerSecret];

[twitterEngine setUsername:username password:password];
[twitterEngine sendUpdate: @"This message was sent via myApp for iOS"];

然而,在我获得accessToken之前,我无法发送推文。这是输出:

Request failed for connectionIdentifier = 5E7C9D2E-1467-45EF-B748-CCBF8F211F8D, error = The operation couldn’t be completed. (HTTP error 401.) ({
    body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <error>Could not authenticate you.</error>\n  <request>/1/statuses/update.xml</request>\n</hash>\n";
    response = "<NSHTTPURLResponse: 0x6e26a10>";
})

如何使用MGTwitterEngine发送推文? (没有手动从dev.twitter.com获取我的访问令牌,因为其他用户也应该能够自己发送推文)

1 个答案:

答案 0 :(得分:0)

我有类似的问题,经过几个小时的搜索,我找到了解决方案。 仅当用户通过检查引擎是否已授权登录时,才会显示我的注销按钮。然后在用户点击按钮后,这是执行的代码。

- (void)logoutTwitter {

if(![_engine isAuthorized]){  
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];  

    if (controller){  
        [self presentModalViewController: controller animated: YES];  
    } 
} else {
    [_engine clearAccessToken]; //This is a method called from SA_OAuthTwitterEngine.h

}