Twitter + oAuth Nightmare

I am trying to implement a custom Twitter login mode (I don't want this UIWebView). I have downloaded many classes and still have a nightmare. Now I'm trying to make Twitter + oAuth work. Here is the demo code (which works):

_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;

[_engine requestRequestToken];

UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];

if (controller) 
    [self presentModalViewController: controller animated: YES];
else
    [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];

Now, what I want to do is replace this SA_OAuthTwitterController with custom UITextFields. So I am trying to do this:

_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;

[_engine requestRequestToken];
[_engine requestAccessToken];
[_engine setUsername:@"username" password:@"password"];
[_engine sendUpdate:@"tweet"];

But I get error 401 all the time. I probably skipped the step. Is anyone

+3
source share
3 answers

I think you are missing a bit, SA_OAuthTwitterEngine.m: 103:

    //This generates a URL request that can be passed to a UIWebView. It will open a page in which the user must enter their Twitter creds to validate
- (NSURLRequest *) authorizeURLRequest {
        if (!_requestToken.key && _requestToken.secret) return nil;     // we need a valid request token to generate the URL

        OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL: self.authorizeURL consumer: nil token: _requestToken realm: nil signatureProvider: nil] autorelease];     

        [request setParameters: [NSArray arrayWithObject: [[[OARequestParameter alloc] initWithName: @"oauth_token" value: _requestToken.key] autorelease]]];   
        return request;
}

"" , , twitter . , setUsername, , - . . SA_OAuthTwitterEngine.m: 185

//
// access token callback
// when twitter sends us an access token this callback will fire
// we store it in our ivar as well as writing it to the keychain
// 
- (void) setAccessToken: (OAServiceTicket *) ticket withData: (NSData *) data {
        if (!ticket.didSucceed || !data) return;

        NSString *dataString = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
        if (!dataString) return;

        if (self.pin.length && [dataString rangeOfString: @"oauth_verifier"].location == NSNotFound) dataString = [dataString stringByAppendingFormat: @"&oauth_verifier=%@", self.pin];

        NSString                                *username = [self extractUsernameFromHTTPBody:dataString];

        if (username.length > 0) {
                [[self class] setUsername: username password: nil];
                if ([_delegate respondsToSelector: @selector(storeCachedTwitterOAuthData:forUsername:)]) [(id) _delegate storeCachedTwitterOAuthData: dataString forUsername: username];
        }

        [_accessToken release];
        _accessToken = [[OAToken alloc] initWithHTTPResponseBody:dataString];
}

, :

, SA_OAuthTwitterController - SA_OAuthTwitterController.m: 156

#pragma mark Webview Delegate stuff
- (void) webViewDidFinishLoad: (UIWebView *) webView {
        NSError *error;
        NSString *path = [[NSBundle mainBundle] pathForResource: @"jQueryInject" ofType: @"txt"];
    NSString *dataSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

    if (dataSource == nil) {
        NSLog(@"An error occured while processing the jQueryInject file");
    }

        [_webView stringByEvaluatingJavaScriptFromString:dataSource]; //This line injects the jQuery to make it look better

        NSString                                        *authPin = [[_webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('oauth_pin').innerHTML"] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if (authPin.length == 0) authPin = [[_webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('oauth_pin').getElementsByTagName('a')[0].innerHTML"] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

        [_activityIndicator stopAnimating];
        if (authPin.length) {
                [self gotPin: authPin];
        } 
        if ([_webView isLoading] || authPin.length) {
                [_webView setHidden:YES];
        } else {
                [_webView setHidden:NO];
        }
}

, .

+1

, , -, . , -. , , , OAuth . , - (Vimeo), :

  • , - . , .

  • , .

  • Vimeo, . "", , .

  • , , . - , Vimeo.

OAuth - , .; -)

+2

You must use xAuth for custom user / password controls. But this requires permission from Twitter.

+1
source

Source: https://habr.com/ru/post/1724462/


All Articles