Getting 'Refreshing due to a 401' when trying to connect using remote_api
<p>I am trying to connect to the production datastore running on Google App Engine based on <a href="https://cloud.google.com/appengine/docs/python/tools/remoteapi#enabling_remote_api_access_in_your_app" rel="nofollow">https://cloud.google.com/appengine/docs/python/tools/remoteapi#enabling_remote_api_access_in_your_app</a> and <a href="http://stackoverflow.com/questions/33378616/appengine-remote-api-returning-401-and-too-many-auth">AppEngine - Remote API returning 401 and too-many-auth</a> and <a href="http://stackoverflow.com/questions/32567357/gae-remote-api-and-application-default-credentials">GAE: remote_api and Application Default Credentials</a> and others.</p>

<p>This is my code to connect to Google App Engine Datastore</p>

<pre><code>try:
    import dev_appserver
    dev_appserver.fix_sys_path()
except ImportError:
    print('Please make sure the App Engine SDK is in your PYTHONPATH.')
    raise

from google.appengine.ext.remote_api import remote_api_stub
import os

class RemoteApi:
    @staticmethod
    def use_remote_datastore():
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "my-appengine-default-service-account.json"
        project_id = 'myapp'
        project_address = '{}.appspot.com'.format(project_id)
        RemoteApi.connect2(project_address)

    @staticmethod
    def auth_func2():
        return ('myuser@myemail.com','mypassword')

    @staticmethod
    def connect2(project_address):
            remote_api_stub.ConfigureRemoteApiForOAuth(
            project_address,
            '/_ah/remote_api', secure=True)
</code></pre>

<p>But I am getting the error </p>

<pre><code>NotSupportedOnThisPlatform
</code></pre>

<p>If I then set </p>

<pre><code>secure=False
</code></pre>

<p>I then get</p>

<pre><code> INFO     2016-10-01 23:35:32,727 client.py:546] Attempting refresh to obtain initial access_token
....
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dummy_thread.py", line 73, in allocate_lock
return LockType()
RuntimeError: maximum recursion depth exceeded
</code></pre>

<p>I tried running</p>

<pre><code>gcloud auth login
</code></pre>

<p>and creating a new service account which are both suggested here <a href="http://stackoverflow.com/questions/33378616/appengine-remote-api-returning-401-and-too-many-auth">AppEngine - Remote API returning 401 and too-many-auth</a></p>

<p>Any ideas what I am doing wrong?</p>

<p>You have mentioned auth_func2 but have not used it, according to remote_api updates without this information everytime with the oauth request, a connection is not possible. </p>

<p>change your <code>connect2</code> method and try this - </p>

<pre><code>@staticmethod
def connect2(project_address):
    remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', auth_func2, project_address)
</code></pre>

<p>P.S - I am assuming your <code>project_address</code> is right and without 'http://'</p>

python google-app-engine google-oauth2