ios - MGTwitterEngine for iOS : how to get accessToken? 错误 401

标签 ios authentication twitter access-token mgtwitterengine

我正在为我的 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"];

但是,在获得 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 手动获取我的访问 token ,因为其他用户也应该能够从他们自己发送推文)

最佳答案

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

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

关于ios - MGTwitterEngine for iOS : how to get accessToken? 错误 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9718562/

相关文章:

java - jtwitter OAuth 切换如何?

php - Twitter API - 显示多个指定用户的最新推文

api - 更改Twitter流过滤器关键字,而无需重新打开流

ios - 更改 NSLayoutConstraint 时的 SWIFT 动画

ios - 来自 NSData 的 NSString 为 nil

python - 我无法进行基本登录 - 找不到对象

PHP:基于 JWT 的身份验证

iphone - WebSocket 在 Objective-C 中的使用示例

ios - 如何告诉用户你的 iOS 应用程序需要多少内存

php - Laravel JWT 多页面结构