简体   繁体   English

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

[英]MGTwitterEngine for iOS : how to get accessToken? Error 401

I'm using MGTwitterEngine library for my iOS app to post tweets. 我正在为我的iOS应用程序使用MGTwitterEngine库发布推文。

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"];

However I can't send tweet until I get accessToken. 但是,直到获得accessToken,我才能发送tweet。 Here's output: 输出如下:

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>";
})

How can I sent tweet using MGTwitterEngine? 如何使用MGTwitterEngine发送推文? (without manually getting my access token from dev.twitter.com because other users should be able to send tweets from themselves too) (无需从dev.twitter.com手动获取我的访问令牌,因为其他用户也应该也可以从自己发送推文)

I had a similar problem and after many hours of searching I found the solution. 我遇到了类似的问题,经过数小时的搜索,我找到了解决方案。 My logout button is showing only when the user is logged in by checking if engine isAuthorized. 仅当通过检查引擎是否已授权用户登录时,我的注销按钮才会显示。 Then after the user taps the button this is the code which is executed. 然后,在用户点击按钮之后,这就是执行的代码。

- (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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM