<?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 Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638501#M29418</link>
    <description>&lt;P&gt;OK I now: the automatic token refreshing is not a new topic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the use case that generate my problem: let's say that we want extract data from Dropbox. Below you can find the code: for the first time works perfectly: in fact 1) the user goes to the generated link; 2) after allow the app coping and pasting the authorization code in the input box.&lt;/P&gt;
&lt;P&gt;The problem arise when some hours after the user wants to do the same operation. How to avoid or by-pass the newly generation of authorization code and go straight to the operation?&lt;/P&gt;
&lt;P&gt;As you can see in the code in a short period is possible reinject the auth code inside the code (commented in the code). But after 1 hour or more this is not loger possible.&lt;/P&gt;
&lt;P&gt;Any help is welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python3

import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect

'''
Populate your app key in order to run this locally
'''
APP_KEY = ""

auth_flow = DropboxOAuth2FlowNoRedirect(APP_KEY, use_pkce=True, token_access_type='offline')

target='/DVR/DVR/'

authorize_url = auth_flow.start()
print("1. Go to: " + authorize_url)
print("2. Click \"Allow\" (you might have to log in first).")
print("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()
# auth_code="3NIcPps_UxAAAAAAAAAEin1sp5jUjrErQ6787_RUbJU"

try:
    oauth_result = auth_flow.finish(auth_code)
except Exception as e:
    print('Error: %s' % (e,))
    exit(1)

with dropbox.Dropbox(oauth2_refresh_token=oauth_result.refresh_token, app_key=APP_KEY) as dbx:
    dbx.users_get_current_account()
    print("Successfully set up client!")
   
      
    for entry in dbx.files_list_folder(target).entries:
        print(entry.name)
    def dropbox_list_files(path):
        try:
            files = dbx.files_list_folder(path).entries
            files_list = []
            for file in files:
                if isinstance(file, dropbox.files.FileMetadata):
                    metadata = {
                        'name': file.name,
                        'path_display': file.path_display,
                        'client_modified': file.client_modified,
                        'server_modified': file.server_modified
                    }
                    files_list.append(metadata)

            df = pd.DataFrame.from_records(files_list)
            return df.sort_values(by='server_modified', ascending=False)

        except Exception as e:
            print('Error getting list of files from Dropbox: ' + str(e))

    # function to get the list of files in a folder
    def create_links(target, csvfile):
        filesList = []
        print("creating links for folder " + target)
        files = dbx.files_list_folder('/'+target)
        filesList.extend(files.entries)
        print(len(files.entries))

        while(files.has_more == True) :
            files = dbx.files_list_folder_continue(files.cursor)
            filesList.extend(files.entries)
            print(len(files.entries))

        for file in filesList :
            if (isinstance(file, dropbox.files.FileMetadata)) :
                filename = file.name + ',' + file.path_display + ',' + str(file.size) + ','
                link_data = dbx.sharing_create_shared_link(file.path_lower)
                filename += link_data.url + '\n'
                csvfile.write(filename)
                print(file.name)

            else :
                create_links(target+'/'+file.name, csvfile)

    # create links for all files in the folder belgeler 
    create_links(target, open('links.csv', 'w', encoding='utf-8'))

listing = dbx.files_list_folder(target)
# todo: add implementation for files_list_folder_continue

for entry in listing.entries:
    if entry.name.endswith(".pdf"):
        # note: this simple implementation only works for files in the root of the folder
        res = dbx.sharing_get_shared_links(
           target + entry.name)
          
        # f.write(res.content)
    
        print('\r', res)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Nov 2022 22:51:34 GMT</pubDate>
    <dc:creator>mauro991</dc:creator>
    <dc:date>2022-11-22T22:51:34Z</dc:date>
    <item>
      <title>Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638501#M29418</link>
      <description>&lt;P&gt;OK I now: the automatic token refreshing is not a new topic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the use case that generate my problem: let's say that we want extract data from Dropbox. Below you can find the code: for the first time works perfectly: in fact 1) the user goes to the generated link; 2) after allow the app coping and pasting the authorization code in the input box.&lt;/P&gt;
&lt;P&gt;The problem arise when some hours after the user wants to do the same operation. How to avoid or by-pass the newly generation of authorization code and go straight to the operation?&lt;/P&gt;
&lt;P&gt;As you can see in the code in a short period is possible reinject the auth code inside the code (commented in the code). But after 1 hour or more this is not loger possible.&lt;/P&gt;
&lt;P&gt;Any help is welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python3

import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect

'''
Populate your app key in order to run this locally
'''
APP_KEY = ""

auth_flow = DropboxOAuth2FlowNoRedirect(APP_KEY, use_pkce=True, token_access_type='offline')

target='/DVR/DVR/'

authorize_url = auth_flow.start()
print("1. Go to: " + authorize_url)
print("2. Click \"Allow\" (you might have to log in first).")
print("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()
# auth_code="3NIcPps_UxAAAAAAAAAEin1sp5jUjrErQ6787_RUbJU"

try:
    oauth_result = auth_flow.finish(auth_code)
except Exception as e:
    print('Error: %s' % (e,))
    exit(1)

with dropbox.Dropbox(oauth2_refresh_token=oauth_result.refresh_token, app_key=APP_KEY) as dbx:
    dbx.users_get_current_account()
    print("Successfully set up client!")
   
      
    for entry in dbx.files_list_folder(target).entries:
        print(entry.name)
    def dropbox_list_files(path):
        try:
            files = dbx.files_list_folder(path).entries
            files_list = []
            for file in files:
                if isinstance(file, dropbox.files.FileMetadata):
                    metadata = {
                        'name': file.name,
                        'path_display': file.path_display,
                        'client_modified': file.client_modified,
                        'server_modified': file.server_modified
                    }
                    files_list.append(metadata)

            df = pd.DataFrame.from_records(files_list)
            return df.sort_values(by='server_modified', ascending=False)

        except Exception as e:
            print('Error getting list of files from Dropbox: ' + str(e))

    # function to get the list of files in a folder
    def create_links(target, csvfile):
        filesList = []
        print("creating links for folder " + target)
        files = dbx.files_list_folder('/'+target)
        filesList.extend(files.entries)
        print(len(files.entries))

        while(files.has_more == True) :
            files = dbx.files_list_folder_continue(files.cursor)
            filesList.extend(files.entries)
            print(len(files.entries))

        for file in filesList :
            if (isinstance(file, dropbox.files.FileMetadata)) :
                filename = file.name + ',' + file.path_display + ',' + str(file.size) + ','
                link_data = dbx.sharing_create_shared_link(file.path_lower)
                filename += link_data.url + '\n'
                csvfile.write(filename)
                print(file.name)

            else :
                create_links(target+'/'+file.name, csvfile)

    # create links for all files in the folder belgeler 
    create_links(target, open('links.csv', 'w', encoding='utf-8'))

listing = dbx.files_list_folder(target)
# todo: add implementation for files_list_folder_continue

for entry in listing.entries:
    if entry.name.endswith(".pdf"):
        # note: this simple implementation only works for files in the root of the folder
        res = dbx.sharing_get_shared_links(
           target + entry.name)
          
        # f.write(res.content)
    
        print('\r', res)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2022 22:51:34 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638501#M29418</guid>
      <dc:creator>mauro991</dc:creator>
      <dc:date>2022-11-22T22:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638649#M29420</link>
      <description>&lt;P&gt;Hi &lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1593444"&gt;@mauro991&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;The code you are talking about is for one time use. &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt; This code is used as a confirmation when refresh token (and others) are going requested. Short after that it's no more valid. You need to keep the received refresh token and reuse it instead of the confirmation code. &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; Refresh token doesn't expire automatically, but you don't even try to keep it somehow. Why are you requesting refresh token (offline access) while you don't use it actually? 🤷&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 01:48:00 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638649#M29420</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2022-11-23T01:48:00Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638687#M29422</link>
      <description>&lt;P&gt;Thanks for your answer but I can't solve the problem. May be that I am a bit confused, but &lt;A href="https://dropbox.tech/developers/using-oauth-2-0-with-offline-access" target="_self"&gt;according this official post&lt;/A&gt;&amp;nbsp;I read "&lt;SPAN&gt;If your app needs to be able to operate long-term without the user present" read how to use “offline” access.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In any case can show me a code example where I can receive refresh token and reuse it instead using confirmation code?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Mauro&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 07:14:24 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638687#M29422</guid>
      <dc:creator>mauro991</dc:creator>
      <dc:date>2022-11-23T07:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638702#M29423</link>
      <description>&lt;P&gt;Hi &lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1593444"&gt;@mauro991&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Yes, exactly, the reffered post is correct. &lt;A href="https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Issue-in-generating-access-token/m-p/592921/highlight/true#M27586" target="_blank" rel="noopener"&gt;One of my posts&lt;/A&gt; together with the interest to the matter provoked 'official post'. As result of setting "offline" mode, you are receiving refresh token and... instead using it as such (long lived), you are using it as a short lived one! This is your issue. There is no any restrictions how to keep it. It's your design decision.&lt;/P&gt;&lt;P&gt;Hope this sheds some light.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 09:21:38 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/638702#M29423</guid>
      <dc:creator>Здравко</dc:creator>
      <dc:date>2022-11-23T09:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Automatic Refresh_token Using oauth-2.0 with offlineaccess</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/639012#M29446</link>
      <description>&lt;P&gt;[Cross-linking for reference: &lt;A href="https://stackoverflow.com/questions/74534382/dropbox-automatic-refresh-token-using-oauth-2-0-with-offlineaccess" target="_blank"&gt;https://stackoverflow.com/questions/74534382/dropbox-automatic-refresh-token-using-oauth-2-0-with-offlineaccess&lt;/A&gt; ]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://www.dropboxforum.com/t5/user/viewprofilepage/user-id/1593444"&gt;@mauro991&lt;/a&gt; Здравко is correct; refresh tokens are long-lived, so you should re-use it. I see in your code you are already requesting offline access and getting back a refresh token. You should store and re-use that resulting refresh token for the user instead of having them process the authorization flow again each time. The dropbox.Dropbox client will handle the refresh process using the supplied refresh token automatically.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 14:05:52 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Python-Automatic-Refresh-token-Using-oauth-2-0-with/m-p/639012#M29446</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2022-11-24T14:05:52Z</dc:date>
    </item>
  </channel>
</rss>

