2012-04-30 26 views
1

如何在ios App中的Google身份驗證之後繼續?當我允許訪問我的應用程序。出現的窗口是「請複製此代碼,切換到您的應用程序並將其複製到:」。我不知道如何從這裏開始。 這是我的代碼編寫如何在ios App中的Google身份驗證之後繼續

SEL finishedSel = @selector(viewController:finishedWithAuth:error:); 

viewController = [[GTMOAuth2ViewControllerTouch 
            controllerWithScope:scope 
             clientID:clientID 
             clientSecret:clientSecret 
             keychainItemName:nil 
             delegate:self              finishedSelector:finishedSel]autorelease]; 


-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
     finishedWithAuth:(GTMOAuth2Authentication *)auth 
       error:(NSError *)error { 
    if (error != nil) { 
     // Authentication failed 
    } else { 
     // Authentication succeeded 
    } 
} 
+0

您是否得到您的解決方案....對於您的職位 – ChenSmile

回答

0

檢查此鏈接gtm-oauth2

,這裏是finishedWithAuth

(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { 
     if (error != nil) { 
      // Authentication failed (perhaps the user denied access, or closed the 
      // window before granting access) 
      NSLog(@"Authentication error: %@", error); 
      NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey 
      if ([responseData length] > 0) { 
       // show the body of the server's authentication failure response 
       NSString *str = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]; 
       NSLog(@"%@", str); 
      } 
      self.auth = nil; 
     } else { 
      // Authentication succeeded 
      // 
      // At this point, we either use the authentication object to explicitly 
      // authorize requests, like 
      // 
      // [auth authorizeRequest:myNSURLMutableRequest 
      //  completionHandler:^(NSError *error) { 
      //   if (error == nil) { 
      //   // request here has been authorized 
      //   } 
      //  }]; 
      // 
      // or store the authentication object into a fetcher or a Google API service 
      // object like 
      // 
      // [fetcher setAuthorizer:auth]; 
      // save the authentication object 
      self.auth = auth; 
     } 
} 
+0

但如何給下一個API請求並獲取數據?這是我堅持的問題。 – ajith

+0

@ajith同樣的問題,我在GTMHTTPFetcher.m中記錄下載的數據,我得到了access_token,token_type,expires_in,id_token,refresh_token,id,email,verified_email。我沒有收到我喜歡的數據。我喜歡訪問AdSense數據,我將範圍設置爲adsense,所以問題不在範圍內。你有沒有找到修復程序? –

+0

@mahmoud iphase one prblm。第一次登錄的G +和第二時間我得到一個收到error..Received錯誤(空)和AUTH對象GTMOAuth2Authentication 0x884c920:{的accessToken = 「ya29.AHES6ZQcL6Z1tBymq-8Zd_YSsm1aH81UQ3eET4vHUndzRX0JkHJNaQ」,refreshToken = 「1/4HFOjk6S84wCTYoFmwqgPd0S8em7Fqg4-y4xsQZCRgk」,EXPIRATIONDATE = 「2013-08-19 12:11:43 +0000」}所以plz幫助我 – Raju

4

的示例代碼讓
你可以用它來獲得用戶的認證後,數據,見代碼

NSString *urlStr = @"https://www.googleapis.com/oauth2/v1/userinfo?alt=json"; 
NSURL *url = [NSURL URLWithString:urlStr]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[self.auth authorizeRequest:request 
       completionHandler:^(NSError *error) { 
        NSString *output = nil; 
        if (error) { 
         output = [error description]; 
        } else { 
         // Synchronous fetches like this are a really bad idea in Cocoa applications 
         // 
         // For a very easy async alternative, we could use GTMHTTPFetcher 
         NSURLResponse *response = nil; 
         NSData *data = [NSURLConnection sendSynchronousRequest:request 
                  returningResponse:&response 
                     error:&error]; 
         if (data) { 
          // API fetch succeeded 
          output = [[[NSString alloc] initWithData:data 
                  encoding:NSUTF8StringEncoding] autorelease]; 

          NSLog(@"output:%@",output); 


         } else { 
          // fetch failed 
          output = [error description]; 
         } 
        } 

       }]; 

請注意,您需要添加此網址的範圍https://www.googleapis.com/auth/userinfo.profile