Использование вызовов RestAPI с MGTwitterEngine + Oauth - PullRequest
0 голосов
/ 27 февраля 2012

Я использую MGTwitterEngine + Oauth для интеграции Twitter в iPhone.Мне нужно опубликовать статус и статус со СМИ в твиттере. При поиске я узнал, что это возможно с помощью REST API.https://dev.twitter.com/docs/api

Но я не нашел никаких ссылок, которые могли бы помочь, как использовать этот API после того, как я прошел аутентификацию в твиттере.Итак, что я сделал, я попытался использовать Oauth, но это требует ручного ввода пин-кода, что не приемлемо в моем случае.Так что я это сделал.Я получаю информацию из методов движка mgtwitter: -

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {


    NSLog(@"Inside authenticatedWithUsername");
    NSLog(@"%@",[[_engine tokenOFAuth] key]);
    NSLog(@"%@",[[_engine tokenOFAuth] pin]);
    NSLog(@"%@",[[_engine tokenOFAuth] secret]);
    [_engine getUserInformationFor:username]; 

}
tokenOFAuth is the instance of OAToken.

- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {

    NSLog(@"User Info Received: %@", userInfo);
    NSDictionary *userDict=[userInfo objectAtIndex:0];

    oAuth.user_id=[userDict objectForKey:@"id"];
    oAuth.screen_name=[userDict objectForKey:@"screen_name"];
    oAuth.oauth_token=[[_engine tokenOFAuth]key];
    oAuth.oauth_token_secret=[[_engine tokenOFAuth]secret];
    [oAuth synchronousAuthorizeTwitterTokenWithVerifier:[[_engine tokenOFAuth]pin]];    
    oAuth.oauth_token_authorized=YES;
    [oAuth saveOAuthTwitterContextToUserDefaults:oAuth];

[self sendTwitterTweetMethod];


}

-(void)sendTwitterTweetMethod
{
    [oAuth loadOAuthTwitterContextFromUserDefaults];
        NSString *postUrl = @"https://api.twitter.com/1/statuses/update.json";
        ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:postUrl]];
        NSMutableDictionary *postInfo = [NSMutableDictionary dictionaryWithObject:@"I am testing my app" forKey:@"status"];
        for (NSString *key in [postInfo allKeys]) {
            [request setPostValue:[postInfo objectForKey:key] forKey:key];
        }
        [request addRequestHeader:@"Authorization" value:[oAuth oAuthHeaderForMethod:@"POST" andUrl:postUrl andParams:postInfo]];
        NSLog(@"[oAuth oAuthHeaderForMethod:@"" andUrl:postUrl andParams:postInfo]=%@",[oAuth oAuthHeaderForMethod:@"POST" andUrl:postUrl andParams:postInfo]);
        [request startSynchronous];
        NSLog(@"Status posted. HTTP result code: %d", request.responseStatusCode);
        [request release];


}

But it is giving response:-Status posted. HTTP result code: 401 which means unauthorized.

, пожалуйста, предложите мне использовать этот оставшийся api с twitter + oauth и без ручного ввода пин-кода.Я застрял здесь. Пожалуйста, помогите.Любые предложения будут высоко оценены.

...