我正在尝试将 Google Drive API 实现到已在使用 Google Sign In SDK 的项目中。我已将 Google Drive 的范围添加到 GIDSignIn 单例中,但 Drive API 似乎要求用户再次登录。有没有办法在初次登录时通过 Google Sign In 完成对 Google Drive API 的授权,而不是强制用户登录两次?
我在这里读过一个类似的问题,Can I use google drive sdk with authentication info from google sign-in sdk on iOS? ,但响应从未成功从 Google 登录返回的 GIDAuthentication 创建 Google Drive 所需的 GTMOAuth2Authentication。
我的 iOS 应用也遇到了同样的问题,还查看了类似的问题 Can I use Google Drive SDK with sign in information from Google Sign In SDK in iOS .根据 Eran Marom 的回答,我能够将我的 Google 登录凭据转换为 OAuth2 凭据,我用它来成功访问 Apps Script Execute API。
我曾在 Swift 中工作。
在应用代理中:
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 {
}
//....
在 View Controller 中:
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 userInfoictionary<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...
}
关于ios - 使用 Google 登录实现 Google Drive API - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366955/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |