Git Product home page Git Product logo

swift-google-drive-client's Introduction

Swift Google Drive Client

Swift v5.8 platforms iOS, macOS

Basic Google Drive HTTP API client that does not depend on Google's SDK. No external dependencies.

  • Authorize access
  • List files
  • Get file info
  • Get file (download)
  • Create file (upload)
  • Update file (upload)
  • Delete file
  • Get user info (about)

๐Ÿ“– Usage

Use Swift Package Manager to add the GoogleDriveClient library as a dependency to your project.

Configure OAuth 2.0 Client ID using Google Cloud Console. Use iOS application type.

Configure your application so that it can handle sign-in redirects. For an iOS app, you can do it by adding or modifying CFBundleURLTypes in Info.plist:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string></string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>com.googleusercontent.apps.1234-abcd</string>
    </array>
  </dict>
</array>

Create the client:

import GoogleDriveClient

let client = GoogleDriveClient.Client.live(
  config: .init(
    clientID: "1234-abcd.apps.googleusercontent.com",
    authScope: "https://www.googleapis.com/auth/drive",
    redirectURI: "com.googleusercontent.apps.1234-abcd://"
  )
)

Make sure the redirectURI contains the scheme defined earlier.

The package provides a basic implementation for storing vulnerable data securely in the keychain. Optionally, you can provide your own, custom implementation of a keychain, instead of using the default one.

import GoogleDriveClient

let keychain = GoogleDriveClient.Keychain(
  loadCredentials: { () async -> GoogleDriveClient.Credentials? in
    // load from secure storage and return
  },
  saveCredentials: { (GoogleDriveClient.Credentials) async -> Void in
    // save in secure storage
  },
  deleteCredentials: { () async -> Void in
    // delete from secure storage
  }
)
let client = GoogleDriveClient.Client.live(
  config: .init(...),
  keychain: keychain
)

โ–ถ๏ธ Example

This repository contains an example iOS application built with SwiftUI.

  • Open GoogleDriveClient.xcworkspace in Xcode.
  • Example source code is contained in the Example Xcode project.
  • Run the app using the GoogleDriveClientExampleApp build scheme.
  • The "Example" tab provides UI that uses GoogleDriveClient library.
  • The "Console" tab provides UI for browsing application logs and HTTP requests.

The example app uses Dependencies to manage its own internal dependencies. For more information about the Dependencies library check out official documentation.

๐Ÿ› Project structure

GoogleDriveClient (Xcode Workspace)
 โ”œโ”€ swift-google-drive-client (Swift Package)
 |   โ””โ”€ GoogleDriveClient (Library)
 โ””โ”€ Example (Xcode Project)
     โ””โ”€ GoogleDriveClientExampleApp (iOS Application)

๐Ÿ›  Develop

  • Use Xcode (version โ‰ฅ 14.3.1).
  • Clone the repository or create a fork & clone it.
  • Open GoogleDriveClient.xcworkspace in Xcode.
  • Use the GoogleDriveClient scheme for building the library and running unit tests.
  • If you want to contribute, create a pull request containing your changes or bug fixes. Make sure to include tests for new/updated code.

โ˜•๏ธ Do you like the project?

Buy Me A Coffee

๐Ÿ“„ License

Copyright ยฉ 2023 Dariusz Rybicki Darrarski

License: MIT

swift-google-drive-client's People

Contributors

darrarski avatar idolize avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swift-google-drive-client's Issues

Download a file to destination with progress

I had started to look into this and thought about opening up a PR, but figured I'd open an issue to begin with in case you had some input.

Would be great to add support in order to download files from Google Drive to a destination as downloading using GetFileData means entire data is in memory.

We can download using:

try await urlSession.download(for: request)

Which returns:

/// - Returns: Downloaded file URL and response. The file will not be removed automatically.

https://developer.apple.com/documentation/foundation/urlsession#2934757

However I'm unsure how to get the download progress.

It seems we may be able to take the approach using AsyncBytes, but this still means entire download living in memory.
https://khanlou.com/2021/10/download-progress-with-awaited-network-tasks/

Awesome Library!!

Sorry, I created this by mistake.
I ran into 403 forbidden error, but it was my mistake, setting with the wrong scope.

==========

I tried google official SDK, it raises many concerns.
It set 3 extra scopes by default:

  1. openid
  2. https://www.googleapis.com/auth/userinfo.profile
  3. https://www.googleapis.com/auth/userinfo.email

And these scopes will be shown on the consent screen. and they can not be toggled off.
the screen ends up like this:
https://stackoverflow.com/questions/70765918/google-oauth-consent-screen-how-do-i-know-the-user-grants-the-requested-access

I really love this approach.

Thanks for making this possible. Really hope this Library will be maintained in long term.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.