GAppAuth
这是一个用于处理通过 AppAuth 与 Google 服务(iOS 和 macOS)交互的类。
通过 CocoaPods 进行安装
只需将此依赖项添加到 Podfile 即可
pod GAppAuth
会自动添加到 GTMAppAuth 的循环依赖。
手动添加
将 GTMAppAuth
依赖项添加到您的 Podfile (Cocoapods) 或手动将文件复制到您的项目目录中。将 GAppAuth.swift
添加到您的项目,并按照下面的设置使用与 Google 服务相关的 AppAuth。
iOS
- 在 https://console.developers.google.com 上设置您项目(API & Services -> Credentials -> Create Credentials -> OAuth Client ID -> iOS),以检索 ClientID 和 iOS 方案 URL。
- 根据需要启用 Google API。
- 将ClientId和RedirectUri添加到您的Info.plist文件中
<key>GAppAuth</key>
<dict>
<key>RedirectUri</key>
<string>com.googleusercontent.apps.YOUR-CLIENT-ID:/oauthredirect</string>
<key>ClientId</key>
<string>YOUR-CLIENT-ID.apps.googleusercontent.com</string>
</dict>
- 将自定义URL-Scheme添加到您的项目中
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.YOUR-CLIENT</string>
</array>
</dict>
</array>
macOS
- 在https://console.developers.google.com 设置您的项目(APIs & Services -> Credentials -> Create Credentials -> OAuth Client ID -> Other)以获取ClientId和ClientSecret。
- 根据需要启用 Google API。
- 将ClientId,ClientSecret RedirectUri添加到您的Info.plist中
<key>GAppAuth</key>
<dict>
<key>RedirectUri</key>
<string>com.googleusercontent.apps.YOUR-CLIENT-ID:/oauthredirect</string>
<key>ClientId</key>
<string>YOUR-CLIENT-ID.apps.googleusercontent.com</string>
<key>ClientSecret</key>
<string>YOUR-SECRET</string>
</dict>
- 将自定义URL-Scheme添加到您的项目中
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.YOUR-CLIENT</string>
</array>
</dict>
</array>
注意:请确保已关闭或正确配置沙箱模式,否则无法打开浏览器窗口。
通用
- 为了授权使用任何Google服务,您需要通过
GAppAuth.shared.appendAuthorizationRealm
将相应的权限范围添加到授权请求中(例如,使用kGTLRAuthScopeDrive进行Google Drive访问)。 - 从任何
UIViewController
或NSViewController
开始,通过调用GAppAuth.shared.authorize
启动授权流程。 - 您可能需要在应用程序启动时检索任何现有授权,这可以通过调用
GAppAuth.shared.retrieveExistingAuthorizationState
来完成。 - 您可以通过监听两个闭包来了解任何更改
stateChangeCallback
或错误errorCallback
。
注意:如果用户撤销访问权限,两个回调都会被调用。
didFinishLaunchingWithOptions
中。
5和7的理想位置是在AppDelegate的贡献
随时创建问题或开启一个PR。