2013-03-19 11 views
6

क्वेरी करने के लिए एक सक्रिय एक्सेस टोकन का उपयोग किया जाना चाहिए, मैं खाता फ्रेमवर्क के माध्यम से फेसबुक को एकीकृत कर रहा हूं, मैंने खोज की और इसे करने के कुछ तरीके प्राप्त किए। यह पहली बार काम कर रहा था लेकिन बाद में यह लॉग के नीचे दिखा रहा है और कोई जानकारी नहीं दे रहा है।फेसबुक ios6 में उपयोगकर्ता जानकारी प्राप्त करें:

प्रवेश करें:

Dictionary contains: { 
    error =  { 
     code = 2500; 
     message = "An active access token must be used to query information about the current user."; 
     type = OAuthException; 
    }; 
} 

कोड मैं

ACAccountStore *_accountStore=[[ACAccountStore alloc] init];; 
    ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    // We will pass this dictionary in the next method. It should contain your Facebook App ID key, 
    // permissions and (optionally) the ACFacebookAudienceKey 
    NSArray * permissions = @[@"email"]; 

    NSDictionary *options = @{ACFacebookAppIdKey :@"my app id", 
    ACFacebookPermissionsKey :permissions, 
    ACFacebookAudienceKey:ACFacebookAudienceFriends}; 

    // Request access to the Facebook account. 
    // The user will see an alert view when you perform this method. 
    [_accountStore requestAccessToAccountsWithType:facebookAccountType 
              options:options 
             completion:^(BOOL granted, NSError *error) { 
              if (granted) 
              { 
               // At this point we can assume that we have access to the Facebook account 
               NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType]; 

               // Optionally save the account 
               [_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil]; 

               //NSString *uid = [NSString stringWithFormat:@"%@", [[_accountStore valueForKey:@"properties"] valueForKey:@"uid"]] ; 
               NSURL *requestURL = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:@"me"]]; 

               SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
                         requestMethod:SLRequestMethodGET 
                            URL:requestURL 
                          parameters:nil]; 
               request.account = [accounts lastObject]; 
               [request performRequestWithHandler:^(NSData *data, 
                        NSHTTPURLResponse *response, 
                        NSError *error) { 

                if(!error){ 
                 NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data 
                              options:kNilOptions error:&error]; 
                 NSLog(@"Dictionary contains: %@", list); 
                 userName=[list objectForKey:@"name"]; 
                 NSLog(@"username %@",userName); 

                 userEmailID=[list objectForKey:@"email"]; 
                 NSLog(@"userEmailID %@",userEmailID); 

                 userBirthday=[list objectForKey:@"birthday"]; 
                 NSLog(@"userBirthday %@",userBirthday); 

                 userLocation=[[list objectForKey:@"location"] objectForKey:@"name"]; 
                 NSLog(@"userLocation %@",userLocation); 
                } 
                else{ 
                 //handle error gracefully 
                } 

               }]; 
              } 
              else 
              { 
               NSLog(@"Failed to grant access\n%@", error); 
              } 
             }]; 

किसी भी सुराग मित्र क्या गलत हो रहा है ... धन्यवाद करते थे।

उत्तर

14

समस्या यह थी कि जब मैंने डिवाइस के अंदर अपना फेसबुक सेटिंग बदल दिया एक्सेस टोकन का समय हो गया। इसलिए यदि आप ACAccountStoreDidChangeNotification के लिए सुनते हैं तो आप नवीनीकरण को कॉल कर सकते हैं फॉर अकाउंट: उपयोगकर्ता को अनुमति के लिए संकेत देने के लिए।

नीचे कोड काम कर रहा है और एक शब्दकोश में उपयोगकर्ता की जानकारी प्राप्त कर रहा है।

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountChanged) name:ACAccountStoreDidChangeNotification object:nil]; 


} 

-(void)getUserInfo 
{ 

self.accountStore = [[ACAccountStore alloc]init]; 
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    NSString *key = @"your_app_id"; 
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil]; 


    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion: 
    ^(BOOL granted, NSError *e) { 
     if (granted) { 
      NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType]; 
      //it will always be the last object with single sign on 
      self.facebookAccount = [accounts lastObject]; 
      NSLog(@"facebook account =%@",self.facebookAccount); 
      [self get]; 
     } else { 
      //Fail gracefully... 
      NSLog(@"error getting permission %@",e); 

     } 
    }]; 
} 


-(void)accountChanged:(NSNotification *)notif//no user info associated with this notif 
{ 
    [self attemptRenewCredentials]; 
} 


-(void)attemptRenewCredentials{ 
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){ 
     if(!error) 
     { 
      switch (renewResult) { 
       case ACAccountCredentialRenewResultRenewed: 
        NSLog(@"Good to go"); 
        [self get]; 
        break; 

       case ACAccountCredentialRenewResultRejected: 

        NSLog(@"User declined permission"); 

        break; 

       case ACAccountCredentialRenewResultFailed: 

        NSLog(@"non-user-initiated cancel, you may attempt to retry"); 

        break; 

       default: 
        break; 

      } 
     } 

     else{ 

      //handle error gracefully 

      NSLog(@"error from renew credentials%@",error); 

     } 

    }]; 
} 

-(void)get 
{ 

    NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
              requestMethod:SLRequestMethodGET 
                 URL:requestURL 
               parameters:nil]; 
    request.account = self.facebookAccount; 

    [request performRequestWithHandler:^(NSData *data, 
             NSHTTPURLResponse *response, 
             NSError *error) { 

     if(!error) 
     { 
      NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"Dictionary contains: %@", list); 
     } 
     else{ 
      //handle error gracefully 
      NSLog(@"error from get%@",error); 
      //attempt to revalidate credentials 
     } 

    }]; 

    self.accountStore = [[ACAccountStore alloc]init]; 
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    NSString *key = @"your_app_id"; 
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"friends_videos"],ACFacebookPermissionsKey, nil]; 


    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion: 
    ^(BOOL granted, NSError *e) {}]; 

} 

This one helped me a lot.

+0

"self.facebookAccount" क्या संदर्भित करता है? –

+0

ACAccount * facebook खाता; – BhushanVU

+0

@ बुहक्सन, महान उत्तर !!! लेकिन मैं उपयोगकर्ता की तस्वीर लाने में सक्षम क्यों नहीं हूं? – user1673099

1

आप सत्र

[FBSession openActiveSessionWithReadPermissions:permissions 
              allowLoginUI:YES 
             completionHandler:^(FBSession *session, FBSessionState status, NSError *error){ 
              if (session.isOpen) { 
               switch (status) { 
                case FBSessionStateOpen: 
                  // here you get the token 
                  NSLog(@"%@", session.accessToken); 
                 break; 
                case FBSessionStateClosed: 
                case FBSessionStateClosedLoginFailed: 
                 [[FBSession activeSession] closeAndClearTokenInformation]; 
                 break; 
                default: 
                 break; 
               } // switch 
              }]; 
+0

इसका ताजा बनाया टोकन के माध्यम से हो रहा भी ... आईओएस 6..using खातों के लिए – BhushanVU

+0

संपादित जवाब की जांच – Vinodh

+1

frmwrk – BhushanVU

1

बनाने कृपया अपने कोड का अधिक विवरण देते जरूरत है ... अपने कोड नहीं होने जा करने के लिए session..you एक वैध होना चाहिए लगता है session..and accessToken के लिए अद्वितीय है प्रत्येक उपयोगकर्ता id..in आपके मामले में मुझे लगता है कि सत्र नहीं है..इस तरह यह आपकी पहुंच टोकन को जान सकता है .. इसलिए, आपको यह त्रुटि मिल रही है ... यदि आप टोकन तक पहुंच के बारे में अधिक जानना चाहते हैं .. फेसबुक डेमो प्रोजेक्ट की जांच करें जो एसडीके के साथ है .. आप भी इस माध्यम से जा सकते हैं .. http://developers.facebook.com/docs/concepts/login/access-tokens-and-types/

+0

कोड जो iOS 6 में फेसबुक के लिए उपयोगकर्ता की जानकारी का उपयोग करने की जरूरत है thats। खातों ढांचे का उपयोग करना ... – BhushanVU

+1

@bhuXan k..then मुझे लगता है कि आपके पहुँच टोकन जा समय सीमा समाप्त हो जाता है हो सकता है की वजह से कुछ मुझे अपने पूरे कोड reason..Show को .. – Shivaay

+0

thats पूरे कोड .... मैं उस कोड के अंदर जोड़ा एक बटन का IBAction .. – BhushanVU

संबंधित मुद्दे