2013-10-08 45 views
0

我使用無法從10月起圖形API得到「訪問令牌」 2013

NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/oauth/authorize?client_id=%@&redirect_uri=%@&scope=%@&type=user_agent&display=touch", facebookClientID, redirectUri, extended_permissions]; 

這個URL中FbGraph.m文件的訪問令牌和2013年10月之前,我得到它在這裏

- (void)webViewDidFinishLoad:(UIWebView *)_webView { 

    //get the url string 

    NSString *url_string = [((_webView.request).URL) absoluteString]; 

    //looking for "access_token=" 

    NSRange access_token_range = [url_string rangeOfString:@"access_token="]; 

但現在我無法得到它。

請建議我

感謝

+0

你有沒有試過NSLog url_string?注意facebook的訪問令牌會在某段時間後過期。 –

回答

1

webViewDidFinishLoad試試這個

-(void)webViewDidFinishLoad:(UIWebView *)_webView { 


if (!appDelegate.fbviewcancelled) { 

    /** 
    * Since there's some server side redirecting involved, this method/function will be called several times 
    * we're only interested when we see a url like: http://www.facebook.com/connect/login_success.html#access_token=.......... 
    */ 

    NSString *url_string = [((_webView.request).URL) absoluteString]; 
    NSLog(@"the data url=%@",url_string); 


    //looking for "access_token=" 
    NSRange access_token_range = [url_string rangeOfString:@"access_token="]; 

    //looking for "error_reason=user_denied" 
    NSRange cancel_range = [url_string rangeOfString:@"error_reason=user_denied"]; 

    //it exists? coolio, we have a token, now let's parse it out.... 
    if (access_token_range.length > 0) { 

     //we want everything after the 'access_token=' thus the position where it starts + it's length 
     int from_index = access_token_range.location + access_token_range.length; 
     NSString *access_token = [url_string substringFromIndex:from_index]; 

     //finally we have to url decode the access token 
     access_token = [access_token stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     //remove everything '&' (inclusive) onward... 
     NSRange period_range = [access_token rangeOfString:@"&"]; 

     //move beyond the . 
     access_token = [access_token substringToIndex:period_range.location]; 

     //store our request token.... 
     self.accessToken = access_token; 
     //store accesstoken from here... 
     NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; 
     [defaults setObject:@"linked" forKey:@"facebookstatus"]; 
     [defaults setObject:self.accessToken forKey:@"facebookatoken"]; 

     NSLog(@"NSUser Defulats updated.. facebookstatus=%@ and facebookatoken=%@",[defaults objectForKey:@"facebookstatus"],[defaults objectForKey:@"facebookatoken"]); 


     //remove our window 
     UIWindow* window = [UIApplication sharedApplication].keyWindow; 
     if (!window) { 
      window = [[UIApplication sharedApplication].windows objectAtIndex:0]; 
     } 

     [self.webView removeFromSuperview]; 

     //tell our callback function that we're done logging in :) 
     if ((callbackObject != nil) && (callbackSelector != nil)) { 
      [callbackObject performSelector:callbackSelector]; 
     } 

     //the user pressed cancel 
    } else if (cancel_range.length > 0) { 
     //remove our window 
     UIWindow* window = [UIApplication sharedApplication].keyWindow; 
     if (!window) { 
      window = [[UIApplication sharedApplication].windows objectAtIndex:0]; 
     } 

     [self.webView removeFromSuperview]; 

     //tell our callback function that we're done logging in :) 
     if ((callbackObject != nil) && (callbackSelector != nil)) { 
      [callbackObject performSelector:callbackSelector]; 
     } 
    } 
} 
} 
+0

hiii ...我也使用像這樣的代碼,但我不能夠。你仍然從這個代碼...? – user2857660

+0

hiii ...我也使用像這樣的代碼,但我不能只我得到http://www.facebook.com/connect/login_success.html這個字符串,沒有別的.. NSString * url_string = [((( _webView.request).URL)absoluteString]; ....你仍然從這個代碼得到它...? – user2857660

+0

是的第一我也得到錯誤,但從這行代碼我可以得到它 – iKambad

0

添加剪輯代碼和shouldStartLoadWithRequest貼上的是UIWebView委託方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    // code from webViewDidFinishLoad 
} 

注意:這適用於我。