使用Objective-C在iOS中使用Twitter API连接并不成功。我使用那些代码:
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"");
}}];
在我的AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Twitter
[[Twitter sharedInstance] startWithConsumerKey:@"z0F**************cg" consumerSecret:@"PJu*******************N6W"];
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return [[Twitter sharedInstance] application:app openURL:url options:options];
return NO;
}
我确切地说,openURL永远不会被调用。
我收到了一些我不理解的错误消息:
Error Domain=TWTRNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo={NSLocalizedFailureReason=Twitter API error : <?xml version="1.0" encoding="UTF-8"?>
<hash>
<error>Desktop applications only support the oauth_callback value 'oob'</error>
<request>/oauth/request_token</request>
</hash>
(code (null)), TWTRNetworkingStatusCode=401, NSErrorFailingURLKey=https://api.twitter.com/oauth/request_token, NSLocalizedDescription=Request failed: unauthorized (401)}
AND:
[TwitterKit] did encounter error with message "Error obtaining user auth token.": Error Domain=TWTRLogInErrorDomain Code=-1 "<?xml version="1.0" encoding="UTF-8"?>
<hash>
<error>Desktop applications only support the oauth_callback value 'oob'</error>
<request>/oauth/request_token</request>
</hash>
" UserInfo={NSLocalizedDescription=<?xml version="1.0" encoding="UTF-8"?>
<hash>
<error>Desktop applications only support the oauth_callback value 'oob'</error>
<request>/oauth/request_token</request>
</hash>
}
似乎它没有连接,我在Swift中看到很多东西,但在Objective-C中没有。
提前致谢。
答案 0 :(得分:7)
当我尝试在我的应用中集成Twitter登录时,我收到此错误
解决方案:
1。 当您在Twitter的开发者帐户上创建应用程序时,那个时候Callback Url字段不想清空。 如果此字段为空,那么此问题在此问题上就要克服,请不要将此回调网址字段设为空。
快乐编码...... !!!
答案 1 :(得分:1)
如果您阅读手册https://dev.twitter.com/twitterkit/ios/access-rest-api
检查用户是否已登录:
NSString *userID = [Twitter sharedInstance].sessionStore.session.userID;
TWTRAPIClient *client = [[TWTRAPIClient alloc] initWithUserID:userID];
添加以下行:
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
NSLog(@"signed in as %@", [session userName]);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
对于图片,如果您选中该图片,它会对您有所帮助:Posting image to twitter using twitterkit
[self tweetImage: [UIImage imageNamed:@"buttonRenault.png"]];
答案 2 :(得分:1)
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
TWTRTwitter.sharedInstance().start(withConsumerKey:"xxxx", consumerSecret:"xxxxxxxxxxx")
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return TWTRTwitter.sharedInstance().application(app, open: url, options: options)
}
ViewController
@IBAction func twitterClick(_ sender: Any) {
// Swift
let composer = TWTRComposer()
composer.setText("just setting up my Twitter Kit")
composer.setImage(UIImage(named: "twitterkit"))
// Called from a UIViewController
composer.show(from: self.navigationController!) { (result) in
if (result == .done) {
print("Successfully composed Tweet")
} else {
print("Cancelled composing")
}
}
}
@IBAction func logOutUserTwitter(_ sender: Any) {
// Swift
let store = TWTRTwitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
store.logOutUserID(userID)
}
}
Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-<xxxx></string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
<string>twitterauth</string>
</array>
Settings Twitter page develop:
Callback URL enter http://localhost
https://github.com/twitter/twitter-kit-ios/wiki/Installation
答案 3 :(得分:1)
从2018年6月12日起,回调锁定将不再是可选的。 iOS应用程序的正确回调格式为:
twitterkit-MY_CONSUMER_KEY://
https://developer.twitter.com/en/docs/basics/developer-portal/guides/callback-urls
答案 4 :(得分:0)
在twitter中禁用此功能:启用回拨锁定(建议启用回调锁定以确保应用无法覆盖回调网址) 并将一些内容粘贴到回调网址