菜鸟教程小白 发表于 2022-12-13 15:48:55

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: ?) -&gt; Bool {
    // Initialize sign-in
    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&amp;configureError)
    assert(configureError == nil, &#34;Error configuring Google services: \(configureError)&#34;)

    GIDSignIn.sharedInstance().delegate = self

    let scopes = &#34;https://www.googleapis.com/auth/drive&#34;
    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 = &#34;CLIENT ID&#34;
private let kScriptId = &#34;SCRIPT ID&#34;
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 == &#34;ToggleAuthUINotification&#34;) {
      self.toggleAuthUI()
      if notification.userInfo != nil {
            let userInfo:Dictionary&lt;String,String!&gt; =
                notification.userInfo as! Dictionary&lt;String,String!&gt;
            self.statusText.text = userInfo[&#34;statusText&#34;]
      }
    }
}

func callAppsScript() {

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

    // Create an execution request object.
    var request = GTLObject()
    request.setJSONValue(&#34;APPS_SCRIPT_FUCTION&#34;, forKey: &#34;function&#34;)

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

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]
查看完整版本: ios - 使用 Google 登录实现 Google Drive API - iOS