gpt4 book ai didi

iOS 谷歌驱动器 API。操作无法完成。 (com.google.GTMOAuth2 错误 -1000。)

转载 作者:行者123 更新时间:2023-12-03 16:39:26 26 4
gpt4 key购买 nike

所以一切正常,我喜欢这个 sdk,但是当我推送 auth Controller 时:

GTMOAuth2ViewControllerTouch *authController;
authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];

[self.navigationController pushViewController:authController animated:YES];

然后在不输入用户电子邮件和密码的情况下返回委托(delegate)回调返回错误:

The operation couldn’t be completed. (com.google.GTMOAuth2 error -1000.)

但是我不确定代理是否应该为我返回这个错误。我使用了快速入门示例。

我的问题是它为什么返回错误,这是正确的吗?因为我只是从 auth Controller 弹出回到以前的 Controller ,所以没有任何犯罪行为,我不想处理任何 api 调用。

最佳答案

当窗口过早关闭时,委托(delegate)方法在 NSError 参数中返回错误代码 -1000

这个error.code (-1000) 没什么好担心的,它可以让你看到用户做了什么。如果他们确实在未经授权的情况下返回,您可能需要显示一条消息“您没有授权,您应该使用我们的 Google 强大功能!”,或者您可以简单地忽略它。。。 p>


来自:/Project/Pods/gtm-oauth2/GTMOAuth2Authentication.h

enum {
// Error code indicating that the window was prematurely closed
kGTMOAuth2ErrorWindowClosed = -1000,
kGTMOAuth2ErrorAuthorizationFailed = -1001,
kGTMOAuth2ErrorTokenExpired = -1002,
kGTMOAuth2ErrorTokenUnavailable = -1003,
kGTMOAuth2ErrorUnauthorizableRequest = -1004
};

复制粘贴示例:

适用于新 iOS 开发人员的简单示例。 ( Google's documentation is also fantastic )
使用(例如:子集 /Drive)通过 CocoaPods 安装 Google API:

pod 'Google-API-Client/Drive'

在您的 View Controller 中,添加以下变量:

// access keys (Should be in a seperate class or constants file)
#define kKeychainItemName @"Google Drive Quickstart"
#define kGDClientID @"your-client-id"
#define kGDClientSecret @"your-client-secret"

@interface ClassViewController() {
GTMOAuth2ViewControllerTouch *_authController; // Auth v/c
YourCustomGoogleDriveController *_googleDriveController; // Custom Class
}

接下来我们需要一个方法来创建和返回我们的 Google OAuth2 View Controller,将其添加到同一个类中。

/**
* Create a new instance of our google auth view controller
*/
- (GTMOAuth2ViewControllerTouch *)createAuthController
{
_authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
return _authController;
}

当上面的委托(delegate)方法完成后,它会调用下面的函数(注意上面函数中的delegate: self@selector)。

/**
* Our delegate method, always called, either with auth token or error
*/
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)authResult
error:(NSError *)error
{
if (error != nil) {
// We have some sort of error, handle it or ignore
_googleDriveController.driveService.authorizer = nil;
switch (error.code) {
case kGTMOAuth2ErrorWindowClosed:
// Show a message? : "Sign in for awesomeness!"
NSLog(@"%ld: User closed window prematurely", (long)error.code);
break;
case kGTMOAuth2ErrorAuthorizationFailed:
NSLog(@"%ld: Authorization Failed", (long)error.code);
break;
case kGTMOAuth2ErrorTokenExpired:
NSLog(@"%ld: Token Expired", (long)error.code);
break;
case kGTMOAuth2ErrorTokenUnavailable:
NSLog(@"%ld: Token Unavailable", (long)error.code);
break;
case kGTMOAuth2ErrorUnauthorizableRequest:
NSLog(@"%ld: Unauthorizable Request", (long)error.code);
break;
default:
break;
}
} else {
// Set the result into our `GTLServiceDrive` in our Custom Controller
_googleDriveController.driveService.authorizer = authResult;
NSLog(@"Success! - User Auth'ed!");
}
}

最后我们需要一个 IBAction 或其他东西来调用我们上面的代码:

/**
* Create our auth controller, and push it to the nav stack (assuming we are in one)
*/
- (IBAction)someButtonClicked:(id)sender
{
[self createAuthController]; // Init our auth controller
_authController.title = @"Sign in"; // Set a custom title (if you want)
[self.navigationController pushViewController:_authController animated:YES];
//[self presentViewController:controller animated:YES completion:nil]; // Show Modally
}

关于iOS 谷歌驱动器 API。操作无法完成。 (com.google.GTMOAuth2 错误 -1000。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com