使用OAuthConsumer从Twitter注销

时间:2014-11-15 04:39:05

标签: ios iphone twitter twitter-oauth logout

我使用 OAuthConsumer 在我的iOS项目中集成了Twitter登录功能。 我在视图中有一个Logout按钮,我想在用户按下按钮时将功能添加到从twitter注销。 我已经尝试过将消费者,请求令牌和访问令牌设置为nil值为

这是完整的代码

-(void)InitializeOAConsumer
{
    if(!consumer){
        consumer = [[OAConsumer alloc] initWithKey:twitter_clientId secret:twitter_secret];
    }

    NSURL* requestTokenUrl = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];


    OAMutableURLRequest* requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
                                                                               consumer:consumer
                                                                                  token:nil
                                                                                  realm:nil
                                                                      signatureProvider:nil];
    OARequestParameter* callbackParam = [[OARequestParameter alloc] initWithName:@"oauth_callback" value:twitter_callback];
    [requestTokenRequest setHTTPMethod:@"POST"];
    [requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
    OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
    [dataFetcher fetchDataWithRequest:requestTokenRequest
                             delegate:self
                    didFinishSelector:@selector(didReceiveRequestToken:data:)
                      didFailSelector:@selector(didFailOAuth:error:)];




}



- (void)didReceiveRequestToken:(OAServiceTicket*)ticket data:(NSData*)data {

    NSString* httpBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];

    NSURL* authorizeUrl = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
    OAMutableURLRequest* authorizeRequest = [[OAMutableURLRequest alloc] initWithURL:authorizeUrl
                                                                            consumer:nil
                                                                               token:nil
                                                                               realm:nil
                                                                   signatureProvider:nil];
    NSString* oauthToken = requestToken.key;
    OARequestParameter* oauthTokenParam = [[OARequestParameter alloc] initWithName:@"oauth_token" value:oauthToken];
    [authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];

     [self.webview loadRequest:authorizeRequest];

}

- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {

    NSString* httpBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
    // WebServiceSocket *connection = [[WebServiceSocket alloc] init];
    //  connection.delegate = self;


   // accessToken.verifier

  //  NSString *pdata = [NSString stringWithFormat:@"type=2&token=%@&secret=%@&login=%@", accessToken.key, accessToken.secret, self.isLogin];
    // [connection fetch:1 withPostdata:pdata withGetData:@"" isSilent:NO];









  //  OARequestHeader

    NSURL* accessTokenUrl = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/verify_credentials.json"];

    OAToken *accessToken_new =
    [[OAToken alloc] initWithKey:accessToken.key
                           secret:accessToken.secret];



    OAMutableURLRequest* oauthRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenUrl consumer:consumer token:accessToken_new realm:@"http://api.twitter.com/" signatureProvider:nil];

    NSString *oauthHeader = [oauthRequest
                             valueForHTTPHeaderField:@"Authorization"];
    if (!oauthHeader) {
        [oauthRequest prepare];
        oauthHeader = [oauthRequest valueForHTTPHeaderField:@"Authorization"];
    }


    OARequestParameter* verifierParam = [[OARequestParameter alloc] initWithName:@"X-Verify-Credentials-Authorization" value:oauthHeader];
    [oauthRequest setHTTPMethod:@"GET"];

   // [request addRequestHeader:@"X-Auth-Service-Provider" value:kVerifyUrl];



    [oauthRequest setParameters:[NSArray arrayWithObject:verifierParam]];
    OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
    [dataFetcher fetchDataWithRequest:oauthRequest
                             delegate:self
                    didFinishSelector:@selector(didReceiveUserData:data:)
                      didFailSelector:@selector(didFailOAuth:error:)];



}

- (void)didReceiveUserData:(OAServiceTicket*)ticket data:(NSData*)data{


    NSLog(@"didReceiveUserData");

    NSDictionary *userInfo = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    [self LogoutFromTwitter ];


    NSString *msg = [NSString stringWithFormat:@"Id : %@\nName : %@\nDisplay name : %@\nprofile image : %@",[userInfo objectForKey:@"id"],[userInfo objectForKey:@"name"],[userInfo objectForKey:@"screen_name"],[userInfo objectForKey:@"profile_image_url"]];

    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@""
                              message:msg
                              delegate:[self parentViewController]
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];


}



- (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error {
    // ERROR!

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{

    //pop up view


    [self InitializeOAConsumer];


    [super viewWillAppear:animated];


}

#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    //  [indicator startAnimating];

    NSString *temp = [NSString stringWithFormat:@"%@",request];
    //  BOOL result = [[temp lowercaseString] hasPrefix:@"http://codegerms.com/callback"];
    // if (result) {
    NSRange textRange = [[temp lowercaseString] rangeOfString:twitter_callback];

    if(textRange.location != NSNotFound){


        // Extract oauth_verifier from URL query
        NSString* verifier = nil;
        NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@"&"];
        for (NSString* param in urlParams) {
            NSArray* keyValue = [param componentsSeparatedByString:@"="];
            NSString* key = [keyValue objectAtIndex:0];
            if ([key isEqualToString:@"oauth_verifier"]) {
                verifier = [keyValue objectAtIndex:1];
                break;
            }
        }

        if (verifier) {
            NSURL* accessTokenUrl = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
            OAMutableURLRequest* accessTokenRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenUrl consumer:consumer token:requestToken realm:nil signatureProvider:nil];
            OARequestParameter* verifierParam = [[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier];
            [accessTokenRequest setHTTPMethod:@"POST"];
            [accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
            OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
            [dataFetcher fetchDataWithRequest:accessTokenRequest
                                     delegate:self
                            didFinishSelector:@selector(didReceiveAccessToken:data:)
                              didFailSelector:@selector(didFailOAuth:error:)];
        } else {
            // ERROR!
        }


        return NO;
    }

    return YES;
}

- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
    // ERROR!
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    // [indicator stopAnimating];
}

-(void)LogoutFromTwitter{
    consumer.secret = nil;
    consumer.key = nil;
    consumer = nil;

    requestToken.key  = nil;
    requestToken.secret = nil;
    requestToken.session = nil;
    requestToken = nil;

    [self setAccessToken:nil];

    accessToken.key  = nil;
    accessToken.secret = nil;
    accessToken.session = nil;
    accessToken = nil;
}

但没有运气!

有没有办法使用OAuthConsumer从Twitter帐户注销?

感谢您的任何建议。

0 个答案:

没有答案