簡體   English   中英

如何緩存嵌入在重定向 url 中的訪問令牌?

[英]How can i cache an access token embedded in a redirect url?

我正在嘗試使用本地銀行的 API,該銀行使用 OAuth2 作為其授權方法。 認證過程如下: 用戶點擊按鈕,webview 應用內啟動,用戶被定向到 URL A: "https://{API URL}/{constant key}/{redirect url}" 然后他們必須使用他們的銀行憑證進行身份驗證,下一頁會提示他們輸入 OTP。 If this is successful, the session token will be embedded in the redirect url as such: Redirect URL: https://{redirect url}/{access token}/{type}

如何將此訪問令牌緩存或存儲為我的 flutter 代碼中的變量,以便我可以將其用於其他 API 調用? 我目前正在使用 flutter webview 插件,啟動 webview 並到達不同的 URL 沒有問題,但我似乎找不到存儲令牌的方法。

對於本地存儲:

您可以使用https://pub.dev/packages/shared_preferences ,它使用本地對應項來存儲首選項。 它基本上是一個鍵/值存儲。

包裝 NSUserDefaults(在 iOS 上)和 SharedPreferences(在 Android 上),為簡單數據提供持久存儲。 數據異步保存到磁盤。 兩個平台都不能保證寫入返回后會持久化到磁盤,並且此插件不得用於存儲關鍵數據。

另一種選擇是 SQLite 與此 package: https://pub.dev/packages/sqflite

Flutter 的 SQLite 插件。 支持 iOS 和 Android。

您當然也可以使用 Flutter 的文件 I/O 功能,如文檔中所述: https://flutter.dev/docs/cookbook/persistence/reading-writing-files

另一種方法是使用緩存管理器 package,它將在后台使用應用程序的緩存和 SQLlite。 也可能是一種解決方案,但操作系統可以隨時刪除文件。 參見 package: https://pub.dev/packages/flutter_cache_manager

對於遠程存儲:

Then of course, as most of the flutter developers use firestore ( https://firebase.google.com/docs/firestore ) or cloud storage ( https://firebase.google.com/docs/storage ) from the Firebase Brand,您可以通過這種方式輕松選擇 go。 當然,數據將在雲中。 不知道這是否符合您的安全要求。

很可能我更喜歡使用第一個提到的方法采用本地持久性方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM