GAppAuth 1.6.0

GAppAuth 1.6.0

测试已测试
语言语言 SwiftSwift
许可 BSD-2-Clause
发布最后发布2020年12月
SPM支持 SPM

Jonas-Taha El Sesiy 维护。



 
依赖
GTMAppAuth= 1.1.0
AppAuth= 1.4.0
 

GAppAuth 1.6.0

GAppAuth

pod License Twitter

这是一个用于处理通过 AppAuth 与 Google 服务(iOS 和 macOS)交互的类。

通过 CocoaPods 进行安装

只需将此依赖项添加到 Podfile 即可

pod GAppAuth

会自动添加到 GTMAppAuth 的循环依赖。

手动添加

GTMAppAuth 依赖项添加到您的 Podfile (Cocoapods) 或手动将文件复制到您的项目目录中。将 GAppAuth.swift 添加到您的项目,并按照下面的设置使用与 Google 服务相关的 AppAuth。

iOS

  1. https://console.developers.google.com 上设置您项目(API & Services -> Credentials -> Create Credentials -> OAuth Client ID -> iOS),以检索 ClientID 和 iOS 方案 URL。
  2. 根据需要启用 Google API。
  3. 将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>
  1. 将自定义URL-Scheme添加到您的项目中
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>com.googleusercontent.apps.YOUR-CLIENT</string>
    </array>
  </dict>
</array>

macOS

  1. https://console.developers.google.com 设置您的项目(APIs & Services -> Credentials -> Create Credentials -> OAuth Client ID -> Other)以获取ClientId和ClientSecret。
  2. 根据需要启用 Google API。
  3. 将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>
  1. 将自定义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>

注意:请确保已关闭或正确配置沙箱模式,否则无法打开浏览器窗口。

通用

  1. 为了授权使用任何Google服务,您需要通过GAppAuth.shared.appendAuthorizationRealm将相应的权限范围添加到授权请求中(例如,使用kGTLRAuthScopeDrive进行Google Drive访问)。
  2. 从任何UIViewControllerNSViewController开始,通过调用GAppAuth.shared.authorize启动授权流程。
  3. 您可能需要在应用程序启动时检索任何现有授权,这可以通过调用GAppAuth.shared.retrieveExistingAuthorizationState来完成。
  4. 您可以通过监听两个闭包来了解任何更改stateChangeCallback或错误errorCallback

注意:如果用户撤销访问权限,两个回调都会被调用。

5和7的理想位置是在AppDelegate的didFinishLaunchingWithOptions中。

贡献

随时创建问题或开启一个PR。