<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773905#M33825</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I'm having trouble with managing multiple users in my app using SwiftyDropbox. While I can connect users, their tokens are expiring and not auto-refreshing as expected. Here are some snippets of my implementation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;public func setupWithAppKeyMultiUserDesktop(_ key: String) {
    DropboxClientsManager.setupWithAppKeyMultiUserDesktop(key, tokenUid: nil)
    NSAppleEventManager.shared().setEventHandler(self,
                                                 andSelector: #selector(handleGetURLEvent),
                                                 forEventClass: AEEventClass(kInternetEventClass),
                                                 andEventID: AEEventID(kAEGetURL))
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
    if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)),
       let urlStr = aeEventDescriptor.stringValue, let url = URL(string: urlStr) {
        let oauthCompletion: DropboxOAuthCompletion = {
            if let authResult = $0 {
                switch authResult {
                case .success(let accessToken):
                    let tokenUid = accessToken.uid
                    self.clients[tokenUid] = DropboxClient(accessToken: accessToken.accessToken)
                    self.fetchUserData(tokenUid: tokenUid)
                case .cancel:
                    print("Authorization flow was manually canceled by user!")
                case .error(_, let description):
                    print("Error: \(String(describing: description))")
                }
            }
        }
        DropboxClientsManager.handleRedirectURL(url, includeBackgroundClient: false, completion: oauthCompletion)
        NSApp.activate(ignoringOtherApps: true)
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;private func fetchUserData(tokenUid: String) {
    guard let client = clients[tokenUid] else { return }
    client.users.getCurrentAccount().response { result, error in
        if let userData = result {
            let account = DropboxAccount(tokenUid: tokenUid, fullAccount: userData)
            self.connectedAccounts.append(account)
        } else if let error {
            if case .authError(let authError, _, _, _) = error, case .expiredAccessToken = authError {
                DropboxClientsManager.reauthorizeClient(tokenUid)
                self.fetchUserData(tokenUid: tokenUid)
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Despite the reauthorization logic, tokens are not refreshing correctly. Any advice on how to resolve this issue would be greatly appreciated.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2024 12:31:44 GMT</pubDate>
    <dc:creator>odoerlek</dc:creator>
    <dc:date>2024-05-29T12:31:44Z</dc:date>
    <item>
      <title>Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773905#M33825</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I'm having trouble with managing multiple users in my app using SwiftyDropbox. While I can connect users, their tokens are expiring and not auto-refreshing as expected. Here are some snippets of my implementation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;public func setupWithAppKeyMultiUserDesktop(_ key: String) {
    DropboxClientsManager.setupWithAppKeyMultiUserDesktop(key, tokenUid: nil)
    NSAppleEventManager.shared().setEventHandler(self,
                                                 andSelector: #selector(handleGetURLEvent),
                                                 forEventClass: AEEventClass(kInternetEventClass),
                                                 andEventID: AEEventID(kAEGetURL))
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
    if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)),
       let urlStr = aeEventDescriptor.stringValue, let url = URL(string: urlStr) {
        let oauthCompletion: DropboxOAuthCompletion = {
            if let authResult = $0 {
                switch authResult {
                case .success(let accessToken):
                    let tokenUid = accessToken.uid
                    self.clients[tokenUid] = DropboxClient(accessToken: accessToken.accessToken)
                    self.fetchUserData(tokenUid: tokenUid)
                case .cancel:
                    print("Authorization flow was manually canceled by user!")
                case .error(_, let description):
                    print("Error: \(String(describing: description))")
                }
            }
        }
        DropboxClientsManager.handleRedirectURL(url, includeBackgroundClient: false, completion: oauthCompletion)
        NSApp.activate(ignoringOtherApps: true)
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;private func fetchUserData(tokenUid: String) {
    guard let client = clients[tokenUid] else { return }
    client.users.getCurrentAccount().response { result, error in
        if let userData = result {
            let account = DropboxAccount(tokenUid: tokenUid, fullAccount: userData)
            self.connectedAccounts.append(account)
        } else if let error {
            if case .authError(let authError, _, _, _) = error, case .expiredAccessToken = authError {
                DropboxClientsManager.reauthorizeClient(tokenUid)
                self.fetchUserData(tokenUid: tokenUid)
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Despite the reauthorization logic, tokens are not refreshing correctly. Any advice on how to resolve this issue would be greatly appreciated.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 12:31:44 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773905#M33825</guid>
      <dc:creator>odoerlek</dc:creator>
      <dc:date>2024-05-29T12:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773957#M33826</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;... While I can connect users, their tokens are expiring and not auto-refreshing as expected.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Hi &lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I don't know what you expect to be, but to work refresh process, your Dropbox client object needs to be initialized with refresh token as well as other credentials like application key, not only access token, as you do:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;...&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;...
                    self.clients[tokenUid] = DropboxClient(accessToken: accessToken.accessToken)
...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;Hope this gives direction.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 14:20:07 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773957#M33826</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2024-05-29T14:20:07Z</dc:date>
    </item>
    <item>
      <title>Re: Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773977#M33827</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Generally, in the application, I want to enable the user to log in to multiple Dropbox accounts so that they have access to all of them in one place. Initially, when I set it up for a single account, it worked well. However, after switching to 'setupWithAppKeyMultiUserDesktop', everything works until the access token expires, which is after four hours. After this time, the token does not refresh automatically as it did for a single account, which means I don't have access to the account data (unless I log in again, but as we know, the access token will be new then, but still only valid for 4 hours).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 14:38:02 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773977#M33827</guid>
      <dc:creator>odoerlek</dc:creator>
      <dc:date>2024-05-29T14:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773985#M33828</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;&lt;SPAN&gt;... Initially, when I set it up for a single account, it worked well. However, after switching to 'setupWithAppKeyMultiUserDesktop', everything works until the access token expires, ...&lt;/SPAN&gt;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;, Single user or multi user implementation doesn't affect how every single client object behaves. The only difference is (or has to be at least) that you have one or multiple client objects.&lt;/P&gt;&lt;P&gt;As seems you have changed something else too. I don't know what, but you should know. Dropbox client object may be initialized in different ways as could be seen &lt;A title="DropboxClient" href="https://github.com/dropbox/SwiftyDropbox/blob/master/Source/SwiftyDropbox/Shared/Handwritten/DropboxClient.swift" target="_blank" rel="noopener"&gt;here&lt;/A&gt;. Most of them support automatic refresh (all but one actually). You have selected in your code the &lt;A href="https://github.com/dropbox/SwiftyDropbox/blob/master/Source/SwiftyDropbox/Shared/Handwritten/DropboxClient.swift#L24" target="_blank" rel="noopener"&gt;only&lt;/A&gt; that doesn't support it. 🤷 There is static token initialization - no refresh!&lt;/P&gt;&lt;P&gt;Take a bit more care. &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@41457EF40051AFF130FDBFE21B496926/emoticons/1f609.png" alt=":winking_face:" title=":winking_face:" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 14:50:17 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/773985#M33828</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2024-05-29T14:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Issues with Token Expiry and Auto-Refreshing in SwiftyDropbox for multiusers</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/774011#M33829</link>
      <description>&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1842299"&gt;@odoerlek&lt;/a&gt;&amp;nbsp;Здравко is correct; in order for the SDK to perform the refresh for you automatically, the client needs to have the necessary credentials, such as the refresh token. In your code you are creating DropboxClient objects with only the access token but not the refresh token, so it can't refresh for you automatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I recommend using the client management functionality built in to the SDK whenever possible. For instance, you can use use DropboxClientsManager.reauthorizeClient to switch between stored clients (including their refresh tokens), so that you can then use the DropboxClientsManager.authorizedClient provided by the client. Calling reauthorizeClient will update which client is returned by DropboxClientsManager.authorizedClient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do want to create and manage the clients yourself though, you'll need to make sure you supply the refresh tokens as well, in which case it will catch and handle expired access token errors for you automatically.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2024 15:50:11 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issues-with-Token-Expiry-and-Auto-Refreshing-in-SwiftyDropbox/m-p/774011#M33829</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2024-05-29T15:50:11Z</dc:date>
    </item>
  </channel>
</rss>

