我正在为我的 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/