2017-09-14 186 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