Google Drive API IOS Permissions of GTLRDriveService



我正在使用Google Drive API播放,并尝试构建一个将图片上传到我的Google Drive的简单应用程序。一旦用户登录,该应用程序应该上传图片,但是它给出了

的错误

" 2017-09-14 00:55:20.342237-0400 drivetest [6705:1647551]错误 发生:错误域= com.google.gtlrrorrobjectDomain代码= 403 "许可不足" userInfo = {gtlrStructuredRor = gtlrerrorobject 0x1C4251D30: {消息:"权限不足"错误:[1]代码:403},, nslocalizedDescription =不足许可}"

我试图将其传递给gtlrdriveservice类型的服务到USERETUP类的INITSETUP()函数,但无济于事。有人可以将我指向正确的轨道,即使我正确登录了为什么我的权限不起作用,而我在GTLRDRDRIVESERVICE中传递的部分是在成功登录后运行的代码中。

我实例化了一个用户表对象,我令setupuser = userEtup()setupuser.initsetup(service)

我的userSetup编写了目标c,并且可以正确桥接它,因为我能够在用swift编写的viewController文件中实例化。

usersetup ::::::::

#import "userSetUp.h"
#import <GoogleSignIn/GoogleSignIn.h>
@import GoogleAPIClientForREST;

@implementation userSetUp
- (void) initSetup:(GTLRDriveService *) driveService {

    printf("heloooooaiosuoiadoidauoalo");
    //GTLRDriveService *driveService = [GTLRDriveService new];
    //NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:@"files/apple.jpg"];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"jpg"];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    GTLRDrive_File *metadata = [GTLRDrive_File object];
    metadata.name = @"apple.jpg";
    //metadata.mimeType = @"application/vnd.google-apps.document";
    GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData:fileData
                                                                                   MIMEType:@"image/jpeg"];
    uploadParameters.shouldUploadWithSingleRequest = TRUE;
    GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
                                                                   uploadParameters:uploadParameters];
    query.fields = @"id";
    [driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                         GTLRDrive_File *file,
                                                         NSError *error) {
        if (error == nil) {
            //NSLog(@"File ID %@", file.identifier);
            printf("it worked");
        } else {
            NSLog(@"An error occurred: %@", error);
        }
    }];

printf("upload complete!");

}
@end

和ViewController。迅速 导入GoogleapicLientForrest 导入Googlesignin 导入Uikit

class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate     {

// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
private let scopes = [kGTLRAuthScopeDriveReadonly]
let service = GTLRDriveService()
let signInButton = GIDSignInButton()
let output = UITextView()
override func viewDidLoad() {
    super.viewDidLoad()
    // Configure Google Sign-in.
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().scopes = scopes
    GIDSignIn.sharedInstance().signInSilently()
    signInButton.frame = CGRect(x: view.frame.width/2 - signInButton.frame.width , y:  view.frame.height/2, width: signInButton.frame.width, height: signInButton.frame.height)
    // Add the sign-in button.
    view.addSubview(signInButton)
    // Add a UITextView to display output.
    output.frame = view.bounds
    output.isEditable = false
    output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
    output.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    output.isHidden = true
    view.addSubview(output);
    //let itsASetup()
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
          withError error: Error!) {
    if let error = error {
        showAlert(title: "Authentication Error", message: error.localizedDescription)
        self.service.authorizer = nil
    } else {
        self.signInButton.isHidden = true
        self.output.isHidden = false
        self.service.authorizer = user.authentication.fetcherAuthorizer()
        listFiles()
    }
}
// List up to 10 files in Drive
func listFiles() {
    let query = GTLRDriveQuery_FilesList.query()
    query.pageSize = 10
    service.executeQuery(query,
                         delegate: self,
                         didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:))
    )
}
// Process the response and display output
@objc func displayResultWithTicket(ticket: GTLRServiceTicket,
                             finishedWithObject result : GTLRDrive_FileList,
                             error : NSError?) {
    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }
    var text = "";
    if let files = result.files, !files.isEmpty {
        text += "Files:n"
        for file in files {
            text += "(file.name!) ((file.identifier!))n"
        }
    } else {
        text += "No files found."
    }
    output.text = text
    let setUpUser = userSetUp()
    setUpUser.initSetup(service)
}

// Helper for showing an alert
func showAlert(title : String, message: String) {
    let alert = UIAlertController(
        title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.alert
    )
    let ok = UIAlertAction(
        title: "OK",
        style: UIAlertActionStyle.default,
        handler: nil
    )
    alert.addAction(ok)
    present(alert, animated: true, completion: nil)
}
}

尝试更改范围:

class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate
{
    // If modifying these scopes, delete your previously saved credentials by
    private let scopes = ["https://www.googleapis.com/auth/drive"]
    ...
}

最新更新