Amit Battan
Amit Battan

Reputation: 3018

Twitter integrated in Iphone App

I used MGTwitterEngine

which try to login then request fail error comes

Error Detail

connectionIdentifier 6260FAFD-AE4E-4F05-AE67-FFA1DA18578F

error description The operation couldn’t be completed. (HTTP error 401.)

error user info ({ body = "\n\n Client application is not permitted to use xAuth.\n /oauth/access_token\n\n"; response = ""; })

Any Idea

-Amit Battan

Upvotes: 1

Views: 1866

Answers (4)

coanor
coanor

Reputation: 3962

The OAuth do not need the permission from Twitter. But XAuth is required.

I'm not using MGTwitterEngine, with the official document of Twitter, I scratched a client with C, to calculated the nonce and signature, then use the tool cURL, then I can get the oauth_token and token_secret.

Additional message about cURL is that you must have created your App in Twitter, you can use the OAuth tool in your App(not required to be the current App your're working on) setting. If the App in twitter is not your current App, you should generate the cURL command line first. Set the Request Settings to POST and the Request URI to http://api.twitter.com/oauth/request_token, leave other setting as default, then click the button on the page bottom, you then see the cURL command, it seems like that:

curl --request 'POST' 'http://api.twitter.com/oauth/request_token' --header 'Authorization: OAuth oauth_consumer_key="your_consumer_key", oauth_nonce="your_random_string", oauth_signature="your_signature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1329273306", oauth_token="451145411-AxGXG8xNdW1Eymwx85hs3sc1OE3NYsXe578AUtPe", oauth_version="1.0"' --verbose

replace the oauth_consumer_key , oauth_timestamp and oauth_signature with yours, delete the oauth_token and its value, copy the command to your cmd or shell and execute it. Another important trick is that, you must explicitly mark the oauth_callback in Authorization, and set the value as oob:

auth_callback="oob"

If your consumer_key and oauth_signature is correct, this steps will not lead to the Client application is not permitted to use xAuth error.

Upvotes: 1

Amit Battan
Amit Battan

Reputation: 3018

@taskinoor there the code I am using ... I have create application on twitter already

Controller .h file

#import <UIKit/UIKit.h>
#import "MGTwitterEngine.h"
@class OAToken;
@interface TwitterTestViewController : UIViewController <MGTwitterEngineDelegate> { 
    MGTwitterEngine *twitterEngine;
    OAToken *token;
}
@end

Controller .m file

#import "TwitterTestViewController.h"
@implementation TwitterTestViewController
- (void)awakeFromNib{
    // Put your Twitter username and password here:
    NSString *username = @"amit_bursys";
    NSString *password = @"mypassword";
    NSString *consumerKey = @"my_key";
    NSString *consumerSecret = @"my_seret";
    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.");
    }
    twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
    [twitterEngine setUsesSecureConnection:NO];
    [twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
    [twitterEngine setUsername:username];
    [twitterEngine getXAuthAccessTokenForUsername:username password:password];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
    [super dealloc];
}
#pragma mark MGTwitterEngineDelegate methods
- (void)requestSucceeded:(NSString *)connectionIdentifier{
    NSLog(@"Request succeeded for connectionIdentifier = %@", connectionIdentifier);
}
- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error{
    NSLog(@"Request failed for connectionIdentifier = %@, error = %@ (%@)", 
          connectionIdentifier, 
          [error localizedDescription], 
          [error userInfo]);
}
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier{
    NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);
}
- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier{
    NSLog(@"Got direct messages for %@:\r%@", connectionIdentifier, messages);
}
- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
{
    NSLog(@"Got user info for %@:\r%@", connectionIdentifier, userInfo);
}
@end

and Code log is

2010-12-21 11:14:31.533 TwitterTest[734:207] Request failed for connectionIdentifier = D28BD476-7315-4510-B51A-968E516D169D, error = The operation couldn’t be completed. (HTTP error 401.) ({
    body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n  <request>/oauth/access_token</request>\n  <error>Client application is not permitted to use xAuth.</error>\n</hash>\n";
    response = "<NSHTTPURLResponse: 0x6022470>";
})

Upvotes: 0

taskinoor
taskinoor

Reputation: 46037

Basic authentication is no longer supported by Twitter. You can use Twitter-oAuth-iPhone. It added oAuth with MGTwitterEngine. There is a demo code project included too. However, you will require SDK >= 3.2 to compile the latest version.

The problem with xAuth that you need to request to the Twitter API team providing detail of the application. In case of oAuth that is not required. You will need to create an application in Twitter Dev and then you need to use the keys in the project. That's all.

Upvotes: 3

Matthew Frederick
Matthew Frederick

Reputation: 22305

MGTwitterEngine is significantly outdated at this point, as it doesn't use xAuth/oAuth. See this tutorial on using MGTwitterEngine with xAuth:

Switching from Basic to xAuth with mgtwitterengine on iPhone

Upvotes: 1

Related Questions