2

既にGoogleログインSDKを使用しているプロジェクトにGoogleドライブAPIを実装しようとしています。 GoogleドライブのスコープをGIDSignInシングルトンに追加しましたが、ドライブAPIでユーザーに再度ログインする必要があるようです。ユーザーに強制的にログインさせるのではなく、Googleログインの初期ログイン時にGoogleドライブAPIの承認を完了する方法はありますか?GoogleログインでGoogleドライブAPIを実装する - iOS

私も同様の質問Can I use google drive sdk with authentication info from google sign-in sdk on iOS?を読んだことがありますが、GoogleドライブがGoogleログインから返されたGIDAuthenticationからGTMOAuth2Authenticationを正常に作成することはありません。

+0

は、Googleが古い看板を卑下していることをhttp://stackoverflow.com/a/36383136/4024736 – Necreaux

+0

はかなり印象を参照してください。新しいフローを使用するためにドキュメンテーションを更新する前に、フローします。 フィードバックをお寄せください(https://developers.google.com/drive/ios/quickstart)。 –

答えて

1

私のiOSアプリで同じ問題が発生しましたが、同様の質問Can I use Google Drive SDK with sign in information from Google Sign In SDK in iOSもチェックアウトしました。 Eran Maromの回答に基づき、私はGoogle Sign In認証情報をOAuth2認証情報に変換することができました.Application Script Execute APIに正常にアクセスするために使用しました。

私はSwiftで働いていました。アプリの委任で

:のViewControllerで

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 { 
} 
//.... 

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... 
} 
関連する問題