ios - 使用 Google 登录实现 Google Drive API - iOS
<p><p>我正在尝试将 Google Drive API 实现到已在使用 Google Sign In SDK 的项目中。我已将 Google Drive 的范围添加到 GIDSignIn 单例中,但 Drive API 似乎要求用户再次登录。有没有办法在初次登录时通过 Google Sign In 完成对 Google Drive API 的授权,而不是强制用户登录两次? </p>
<p>我在这里读过一个类似的问题,<a href="https://stackoverflow.com/questions/30187332/can-i-use-google-drive-sdk-with-authentication-info-from-google-sign-in-sdk-on-i" rel="noreferrer noopener nofollow">Can I use google drive sdk with authentication info from google sign-in sdk on iOS?</a> ,但响应从未成功从 Google 登录返回的 GIDAuthentication 创建 Google Drive 所需的 GTMOAuth2Authentication。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我的 iOS 应用也遇到了同样的问题,还查看了类似的问题 <a href="https://stackoverflow.com/questions/30187332/can-i-use-google-drive-sdk-with-authentication-info-from-google-sign-in-sdk-on-i" rel="noreferrer noopener nofollow">Can I use Google Drive SDK with sign in information from Google Sign In SDK in iOS</a> .根据 Eran Marom 的回答,我能够将我的 Google 登录凭据转换为 OAuth2 凭据,我用它来成功访问 Apps Script Execute API。 </p>
<p>我曾在 Swift 中工作。</p>
<p>在应用代理中:</p>
<pre><code>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
//
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: ?) -> 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 {
}
//....
</code></pre>
<p>在 ViewController 中:</p>
<pre><code>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...
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 Google 登录实现 Google Drive API - iOS,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36366955/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36366955/
</a>
</p>
页:
[1]