I use Twitter-OAuth-iPhone.
When I try to login on twitter with SA_OAuthTwitterEngine
, I received an error 401
I received the delegate call for:
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
but when I try to send a tweet just after:
[_engine sendUpdate: [NSString stringWithFormat:@"Hello World"]];
I received an error 401:
failed with error: Error Domain=HTTP Code=401 "Operation could not be completed. (HTTP error 401.)"
I have check my twitter application setting:
Type: Client
Default Access type: 开发者_高级运维 Read & Write
You should request for xAuth access to api@twitter.com.
HTTP 401 is an “unauthorized” error. Double check your Twitter API settings and make sure you are using the correct values for the kOAuthConsumerSecret
and kOAuthConsumerKey
constants. Also make sure that your Twitter app is registered as “client” and not “browser”.
This might help answer your question: I've figured it out just now:
I think I've used Matt Gemmell and his source code although I may have used Ben Gottlieb, I can't quite remember. Pretty sure it's Matt though.
However what I've also done is create a hidden subview that holds the actual tweeting text field, the tweet button and a return button so the user can then hide the view again.
So to summarise, I have a button which shows the originally hidden subview and while it does this, it authenticates the user. This subview then has the tweet button used to post tweets. Here is the code that I find has worked for me. Now I've only done minimal testing, but I hope it is enough for you guys to go off on your own and take it from here.
-(IBAction)shareTwit:(id)sender { //This shows the tweet view and authorises the user and engine
tweetView.hidden = NO;
if (!_engine) {
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
}
if (![_engine isAuthorized]) {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
-(IBAction)updateTwitter:(id)sender { //This button sends the tweet to Twitter.
if ([_engine isAuthorized]) {
[_engine sendUpdate:tweetTextField.text];
[tweetTextField resignFirstResponder];
confirmation.text = @"Tweet sent successfully."; //This is an optional UILabel, if you would like the user to know if the tweet has been sent
// Currently, I haven't created a method where the user is told the sending failed.
}
else {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
Notice I haven't used a viewDidLoad
, mainly because I didn't agree with it. I wanted the user to tap the shareTwit
button first to activate the authentication. Then within the view launched they can write their tweet. I hope this helps you!
Setup the Callback URL
field in your twitter application profile.
It should be identical with:
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
精彩评论