2017-09-14 159 views
0

我在玩谷歌驱动器API并试图构建一个上传图片到我的谷歌驱动器的简单应用程序。该应用应该在用户登录后上传图片,但它会给出错误Google Drive API IOS GTLRDriveService的权限

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

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

我实例化一个userSetUp对象和我 让setUpUser = userSetUp() setUpUser.initSetup(服务)

我已经userSetUp写在目标C作为例如和如我能够实例化其被正确地桥接在我的viewcontroller文件中写入swift。

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) 
} 
} 

回答

0

试图改变自己的范围,如:

class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate 
{ 
    // If modifying these scopes, delete your previously saved credentials by 

    private let scopes = ["https://www.googleapis.com/auth/drive"] 

    ... 
} 
+1

非常感谢你这个工作! – JustANoob