如何在Google中获取用户信息以及与iOS集成

时间:2014-11-24 08:51:22

标签: ios objective-c xcode uiwebview google-plus

您好我正在集成google plus集成我想获取当前登录的用户信息。我可以在登录后获得令牌,但我不知道如何获取个人信息。 我一步一步登录,因为这是因为我想在没有打开外部浏览器的情况下登录。

UIWebView中打开网址链接,打开登录屏幕,查看已加载。

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webView.tag=99;
NSString *url = [NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%@&redirect_uri=%@&scope=%@&data-requestvisibleactions=%@",[GPPSignIn sharedInstance].clientID,callbakc,scope,visibleactions];


[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

[self.view addSubview:webView];

获取令牌登录的电话

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
 if ([[[request URL] host] isEqualToString:@"localhost"]) {

        // Extract oauth_verifier from URL query
        NSString* verifier = nil;
        NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@"&"];
        for (NSString* param in urlParams) {
            NSArray* keyValue = [param componentsSeparatedByString:@"="];
            NSString* key = [keyValue objectAtIndex:0];
            if ([key isEqualToString:@"code"]) {
                verifier = [keyValue objectAtIndex:1];
                NSLog(@"verifier %@",verifier);
                break;
            }
        }

        if (verifier) {

            NSString *data = [NSString stringWithFormat:@"code=%@&client_id=%@&client_secret=%@&redirect_uri=%@&grant_type=authorization_code", verifier,[GPPSignIn sharedInstance].clientID,secret,callbakc];
            NSString *url = [NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/token"];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
            NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
            receivedData = [[NSMutableData alloc] init];

        } else {
            // ERROR!
        }

        [webView removeFromSuperview];

        return NO;
    }

我收到的回复确实收到了数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[receivedData appendData:data];
NSLog(@"verifier %@",receivedData);}

现在,在令牌之后我想调用登录​​的获取用户信息,请指导我如何实现这个目标,这对我来说非常好。 感谢。

0 个答案:

没有答案