2016-11-21 56 views
0

我需要在我的iOS應用程序中實現twitter登錄,我已經安裝了與fabric SDK的twitter框架。現在,我第一次遇到問題,而登錄英寸我能夠接收令牌的詳細信息Twitter SDK SDK與織物

Twitter的詳細信息:@TestUserApp登錄! (796637020760186880)

驗證令牌:796637020760186880,ieeZM2Mbn3FLutLOcJ1sk7bp83ZSlzU

身份驗證令牌祕密:1G0O5YdF89V0VtoNmdY8W3ui2pkPNvKgtNPbqrleOiIoB

錯誤:錯誤域= TwitterAPIErrorDomain碼= 99「請求失敗: 禁止(403) 「UserInfo = {NSLocalizedFailureReason = Twitter API錯誤 :無法驗證您的憑據(代碼99), TWTRNetworkingStatusCode = 40 3, NSErrorFailingURLKey = https://api.twitter.com/oauth2/token, NSLocalizedDescription =請求失敗:禁止(403)}

全碼:

TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) { 
    if (session) { 

     NSString *authToken = session.authToken; 
     NSString *authTokenSecret = session.authTokenSecret; 
     NSString* userId = session.userID; 
     NSString *message = [NSString stringWithFormat:@"@%@ logged in! (%@) -Auth token:%@ -Auth Token secret:%@", 
          [session userName], [session userID],authToken,authTokenSecret]; 

     // Objective-C 
     TWTRAPIClient *client = [[TWTRAPIClient alloc] init]; 
     [client loadUserWithID:userId completion:^(TWTRUser *user, NSError *error) { 
      if (error == nil) { 
       NSLog(@"Name:%@ -- ScreenName:%@ -- ProfileImageUrl:%@",user.name,user.screenName,user.profileImageURL); 
      } 
      else { 
       NSLog(@"Error:%@",error.description); 
      } 

     }]; 

    } else { 
     NSLog(@"Login error: %@", [error localizedDescription]); 
    } 
}]; 

回答

0

//在應用程序委託

#import <Fabric/Fabric.h> 
#import <TwitterKit/TwitterKit.h> 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[Fabric with:@[[Twitter class]]]; 
} 

//您的控制器.m

#import <TwitterKit/TwitterKit.h> 


[[Twitter sharedInstance] logInWithMethods:TWTRLoginMethodWebBased completion:^(TWTRSession *session, NSError *error) 
{ 
    if (session) 
    { 

     TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser]; 
     NSURLRequest *request = [client URLRequestWithMethod:@"GET" 
                  URL:@"https://api.twitter.com/1.1/account/verify_credentials.json" 
                parameters:@{@"include_email": @"true", @"skip_status": @"true"} 
                 error:nil]; 

     [client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) 
      { 

       if (data) 
       { 
        // handle the response data e.g. 
        NSError *jsonError; 
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; 
        NSLog(@"Twiiter json is ====>%@",json); 

        NSString *userid =[json objectForKey:@"id"]; 
        NSString *strtwitter =[NSString stringWithFormat:@"%@",@"https://twitter.com/intent/user?user_id=",userid]; 
        NSLog(@"strtwitter is ===>%@",strtwitter); 

        [self dataSendToTwitter:json]; 

       } 
       else { 
        NSLog(@"Error: %@", connectionError); 
       } 


      }]; 



    } else { 
     NSLog(@"error: %@", [error localizedDescription]); 
    } 
}];