2

Ich versuche, die Google Drive API in ein Projekt zu implementieren, das bereits das Google Sign In SDK verwendet. Ich habe den Umfang für Google Drive zum GIDSignIn-Singleton hinzugefügt, aber die Drive-API scheint zu erfordern, dass sich der Nutzer erneut anmeldet. Gibt es eine Möglichkeit, die Autorisierung für die Google Drive-API bei der ersten Anmeldung über Google Sign In abzuschließen, anstatt den Nutzer zur zweimaligen Anmeldung zu zwingen?Implementieren der Google Drive API mit Google Anmelden - iOS

Ich habe eine ähnliche Frage hier gelesen, Can I use google drive sdk with authentication info from google sign-in sdk on iOS?, aber die Antwort erstellt nie erfolgreich eine GTMOAuth2Authentication erforderlich von Google Drive von der GIDAuthentication von Google Sign In zurückgegeben.

+0

http://stackoverflow.com/a/36383136/4024736 Siehe – Necreaux

+0

ziemlich unscheinbar, dass Google ist deprecating das alte Zeichen -in Flow, bevor Sie ihre Dokumentation aktualisieren, um den neuen Flow zu verwenden. Ich schlage vor, dass Sie Feedback auf der Dokumentationsseite senden: https://developers.google.com/drive/ios/quickstart –

Antwort

1

Ich stieß auf das gleiche Problem mit meiner iOS-App, und auch die ähnliche Frage Can I use Google Drive SDK with sign in information from Google Sign In SDK in iOS ausgecheckt. Aufgrund der Antwort von Eran Marom konnte ich meine Google-Anmeldeinformationen in eine OAuth2-Anmeldeinformationen umwandeln, mit denen ich erfolgreich auf die Apps Script Execute-API zugreifen konnte.

Ich arbeitete in Swift.

Im App Delegierter:

import GTMOAuth2 

@UIApplicationMain 

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate { 

var window: UIWindow? 

//Create an authorization fetcher, which will be used to pass credentials on to the API request 
var myAuth: GTMFetcherAuthorizationProtocol? = nil 

// [START didfinishlaunching] 
func application(application: UIApplication, 
       didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Initialize sign-in 
    var configureError: NSError? 
    GGLContext.sharedInstance().configureWithError(&configureError) 
    assert(configureError == nil, "Error configuring Google services: \(configureError)") 

    GIDSignIn.sharedInstance().delegate = self 

    let scopes = "https://www.googleapis.com/auth/drive" 
    GIDSignIn.sharedInstance().scopes.append(scopes) 

    return true 
} 
//.... 

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, 
      withError error: NSError!) { 
    if (error == nil) { 

     //sets credentials in fetcher 
myAuth = user.authentication.fetcherAuthorizer() 

     //... 
    } else { 
} 
//.... 

Im Viewcontroller:

import UIKit 
import GoogleAPIClient 
import GTMOAuth2 

@objc(ViewController) 

class ViewController: UITableViewController, GIDSignInUIDelegate { 

private let kClientID = "CLIENT ID" 
private let kScriptId = "SCRIPT ID" 
private let service = GTLService() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    GIDSignIn.sharedInstance().uiDelegate = self 
//... 
} 

func toggleAuthUI() { 
    if (GIDSignIn.sharedInstance().hasAuthInKeychain()){ 

     self.service.authorizer = appDelegate.myAuth 
     //... 
    callAppsScript() 
    } else { 
//... 
    } 

@objc func receiveToggleAuthUINotification(notification: NSNotification) { 
    if (notification.name == "ToggleAuthUINotification") { 
     self.toggleAuthUI() 
     if notification.userInfo != nil { 
      let userInfo:Dictionary<String,String!> = 
       notification.userInfo as! Dictionary<String,String!> 
      self.statusText.text = userInfo["statusText"] 
     } 
    } 
} 

func callAppsScript() { 

    let baseUrl = "https://script.googleapis.com/v1/scripts/\(kScriptId):run" 
    let url = GTLUtilities.URLWithString(baseUrl, queryParameters: nil) 

    // Create an execution request object. 
    var request = GTLObject() 
    request.setJSONValue("APPS_SCRIPT_FUCTION", forKey: "function") 

    // Make the API request. 
    service.fetchObjectByInsertingObject(request, 
     forURL: url, 
     delegate: self, 
     didFinishSelector: "displayResultWithTicket:finishedWithObject:error:") 
} 

func displayResultWithTicket(ticket: GTLServiceTicket, 
          finishedWithObject object : GTLObject, 
               error : NSError?) { 
//Display results... 
}