我正在尝试使用 google-api-objectivec-client library 向用户的 Google+ 帐户中插入“时刻” .我认为身份验证过程正常工作。这主要与我设置 YouTube 身份验证的方式相同,但具有正确的范围和钥匙串(keychain)名称。但是,当我尝试运行查询以插入时,出现以下错误:
Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Unauthorized)"
在更仔细地查看了 Google 的文档 ( here ) 之后,我发现了以下评论:
When authenticating for moments.insert, you must include the
data-requestvisibleactions
parameter to specify which types of App Activities your application will write.
Google 有几个关于如何使用其他编程语言执行此操作的示例,但他们没有针对 objective-c 库的任何示例,而且 objective-c 项目也不包含任何如何执行此操作的示例.
我知道还有另一种身份验证方法,使用 GPPSignIn 按钮,它可以设置操作。但是,我的应用程序正在使用多个其他 Google API 客户端(YouTube、YouTube Analytics 和 URL Shortener)。将 GoogleOpenSource.framework
与其他 Objective-C 库混合使用会导致冲突。所以,我需要使用 GTMOAuth2ViewControllerTouch 类。
我的验证码
GTMOAuth2ViewControllerTouch *viewController =
[GTMOAuth2ViewControllerTouch controllerWithScope:kGTLAuthScopePlusLogin
clientID:kGoogleApiClientId
clientSecret:kGoogleApiClientSecret
keychainItemName:kGooglePlusKeychainName
completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
if (error)
NSLog(@"Error: %@", error.description);
else
app.googlePlusService.authorizer = auth; //this sets a property of my app delegate, to be used elsewhere in the application
}];
[self.navigationController pushViewController:viewController animated:YES];
我用来插入“时刻”的代码
NSString *shareUrl = "http://www.google.com"; //just for this example
GTLPlusMoment *moment = [[GTLPlusMoment alloc] init];
moment.type = @"http://schemas.google.com/AddActivity";
GTLPlusItemScope *target = [[GTLPlusItemScope alloc] init];
target.url = shareUrl;
moment.target = target;
GTLQueryPlus *query =
[GTLQueryPlus queryForMomentsInsertWithObject:moment
userId:@"me"
collection:kGTLPlusCollectionVault];
[app.googlePlusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
id object,
NSError *error) {
if (error) {
NSLog(@"Got bad response from plus.moments.insert: %@", error);
} else {
NSLog(@"Moment inserted: %@",moment.identifier);
}
}];
有没有人成功找到将 data-requestvisibleactions
参数添加到身份验证调用或 queryForMomentsInsertWithObject 方法的位置,以允许他们执行 moments.insert
操作没有收到错误?
谢谢!
最佳答案
您可以在 Authentication
中使用 Java Script 代码添加 data-requestvisibleactions
。一旦您使用 java script
授权
,它将被授权用于所有类型的插入时刻。 Objective c 中没有用于在代码中添加 data-requestvisibleactions
的方法。仅通过Java脚本才能选择该选项。
有关更多脱轨,请参阅 THIS
关于ios - 使用 GTMOAuth2ViewControllerTouch 进行身份验证时执行 moments.insert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447494/