在 iOS 上使用 google api 获取 json 数据时遇到问题
Posted
技术标签:
【中文标题】在 iOS 上使用 google api 获取 json 数据时遇到问题【英文标题】:Trouble fetching json data with google api on iOS 【发布时间】:2013-06-15 00:37:22 【问题描述】:我正在尝试使用此代码从 adsense api 获取 json
#import "ViewController.h"
#import "GTMOAuth2Authentication.h"
#import "GTMOAuth2ViewControllerTouch.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
[self signInToGoogle];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//Creat your Google APP here: https://code.google.com/apis/console/ and get the key and secret
#define GoogleClientID @"xxxxxxxxx"
#define GoogleClientSecret @"xxxxxxxxx"
#define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth"
#define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token"
- (GTMOAuth2Authentication * )authForGoogle
//This URL is defined by the individual 3rd party APIs, be sure to read their documentation
NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];
// We'll make up an arbitrary redirectURI. The controller will watch for
// the server to redirect the web view to this URI, but this URI will not be
// loaded, so it need not be for any actual web page. This needs to match the URI set as the
// redirect URI when configuring the app with Instagram.
NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";
GTMOAuth2Authentication * auth;
auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"lifebeat"
tokenURL:tokenURL
redirectURI:redirectURI
clientID:GoogleClientID
clientSecret:GoogleClientSecret];
auth.scope = @"https://www.googleapis.com/auth/adsense";
return auth;
- (void)signInToGoogle
GTMOAuth2Authentication * auth = [self authForGoogle];
// Display the authentication view
GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth
authorizationURL:[NSURL URLWithString:GoogleAuthURL]
keychainItemName:@"GoogleKeychainName"
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[self.navigationController pushViewController:viewController animated:YES];
- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController
finishedWithAuth:(GTMOAuth2Authentication * )auth
error:(NSError * )error
NSLog(@"finished");
NSLog(@"auth access token: %@", auth.accessToken);
[self.navigationController popToViewController:self animated:NO];
if (error != nil)
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
else
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success Authorizing with Google"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/adsense/v1.1/reports"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil)
NSLog(@"%@", result);
@end
我在应用程序上很好地登录到谷歌并弹出一个说成功的弹出窗口,但是在发生这种情况时,我的 nslog 正在显示这个
2013-06-14 19:28:51.681 Simple-OAuth2[9253:c07] finished
2013-06-14 19:28:51.681 Simple-OAuth2[9253:c07] auth access token: ya29.xxxxxxxxxx
2013-06-14 19:28:51.912 Simple-OAuth2[9253:c07]
error =
code = 401;
errors = (
domain = global;
location = Authorization;
locationType = header;
message = "Login Required";
reason = required;
);
message = "Login Required";
;
如何保持登录状态?
【问题讨论】:
【参考方案1】:您必须传递参数才能访问数据。检查下面的链接会有所帮助。
https://developers.google.com/adsense/management/standard_params https://developers.google.com/adsense/management/v1.1/reference/reports/generate
需要 O-Auth 令牌和 API 密钥才能获得响应。
【讨论】:
以上是关于在 iOS 上使用 google api 获取 json 数据时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 google 联系人 api 在 iOS 应用程序中获取 gmail 联系人?
如何从 Google YouTube Data API OAuth 2 iOS 获取刷新令牌
Google Maps API:获取当前位置 iOS 的坐标
如何在 android 上使用 google map api 获取 google 驾驶指令?