【问题标题】:Google Drive API IOS Permissions of GTLRDriveServiceGTLRDriveService 的 Google Drive API IOS 权限
【发布时间】:2017-09-14 04:58:27
【问题描述】:

我正在使用 google drive API 并尝试构建一个简单的应用程序,将图片上传到我的 google drive。该应用程序应该在用户登录后上传图片,但是它给出了一个错误

"2017-09-14 00:55:20.342237-0400 driveTest[6705:1647551] 错误 发生:错误域=com.google.GTLRErrorObjectDomain 代码=403 “权限不足” 用户信息={GTLRStructuredError=GTLRErrorObject 0x1c4251d30: {消息:“权限不足”错误:[1]代码:403}, NSLocalizedDescription=权限不足}"

我尝试将 GTLRDriveService 类型的服务传递给 userSetUp 类的 initSetup() 函数,但无济于事。有人可以为我指出正确的轨道,为什么即使我已正确登录,我的权限也无法正常工作,而我在 GTLRDriveService 中传递的部分是在成功登录后运行的代码中。

我实例化了一个 userSetUp 对象,然后我 让 setUpUser = userSetUp() setUpUser.initSetup(service)

我已经用objective c 编写了userSetUp,它被正确桥接,因为我能够在我用swift 编写的viewcontroller 文件中实例化它。

用户设置:::::::

#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

还有视图控制器。迅速 导入 GoogleAPIClientForREST 导入谷歌登录 导入 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)
}
}

【问题讨论】:

    标签: objective-c swift xcode authentication google-drive-api


    【解决方案1】:

    尝试更改您的范围,例如:

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

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多