无法从 2013 年 10 月开始从图形 API 获取"access token"



我正在使用

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];

访问令牌FbGraph.m文件中的此 URL,在 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="];

但现在我无法得到它。

请建议我

谢谢

试试这个

-(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];
        }
    }
}
}

webViewDidFinishLoad中添加剪切代码并粘贴到shouldStartLoadWithRequest UIWebView 委托方法:

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

注意:这对我有用。

最新更新