2017-09-14 8 views
0

私はGoogleドライブAPIを使い、Googleドライブに画像をアップロードする簡単なアプリケーションを構築しようとしています。ユーザーがサインインすると、アプリはアップロードされるはずですが、GoogleドライブAPI 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オブジェクトをインスタンス化し、私はuserSetUpなどの客観Cで書かれていると私はそれをインスタンス化することができる午前としてそれが正しくブリッジされsetUpUser = userSetUp() setUpUser.initSetup(サービス)

を聞かせて 迅速に書かれた私の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) 
} 
} 

答えて

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

関連する問題