BlenderKit: fix oauth failing on some connections. This updates the oauth script to last version, and also ensures the login attempt can be properly canceled when it hangs.

This commit is contained in:
Vilém Duha 2019-07-26 12:01:43 +02:00
parent 7ecccf12b8
commit 92830c7e43
4 changed files with 31 additions and 14 deletions

View File

@ -32,6 +32,8 @@ else:
import bpy
import threading
import requests
from bpy.props import (
BoolProperty,
@ -40,16 +42,20 @@ from bpy.props import (
CLIENT_ID = "IdFRwa3SGA8eMpzhRVFMg5Ts8sPK93xBjif93x0F"
PORTS = [62485, 65425, 55428, 49452]
active_authenticator = None
def login_thread(signup=False):
global active_authenticator
r_url = paths.get_oauth_landing_url()
url = paths.get_bkit_url()
thread = threading.Thread(target=login, args=([signup, url, r_url]), daemon=True)
authenticator = oauth.SimpleOAuthAuthenticator(server_url=url, client_id=CLIENT_ID, ports=PORTS)
#we store authenticator globally to be able to ping the server if connection fails.
active_authenticator = authenticator
thread = threading.Thread(target=login, args=([signup, url, r_url, authenticator]), daemon=True)
thread.start()
def login(signup, url, r_url):
authenticator = oauth.SimpleOAuthAuthenticator(server_url=url, client_id=CLIENT_ID, ports=PORTS)
def login(signup, url, r_url, authenticator):
auth_token, refresh_token = authenticator.get_new_token(register=signup, redirect_url=r_url)
utils.p('tokens retrieved')
tasks_queue.add_task((write_tokens, (auth_token, refresh_token)))
@ -140,8 +146,16 @@ class CancelLoginOnline(bpy.types.Operator):
return True
def execute(self, context):
global active_authenticator
preferences = bpy.context.preferences.addons['blenderkit'].preferences
preferences.login_attempt = False
try:
if active_authenticator is not None:
requests.get(active_authenticator.redirect_uri)
active_authenticator = None
except Exception as e:
print('stopped login attempt')
print(e)
return {'FINISHED'}

View File

@ -38,6 +38,8 @@ class SimpleOAuthAuthenticator(object):
"client_id": self.client_id,
"scopes": "read write",
}
if hasattr(self, 'redirect_uri'):
data["redirect_uri"] = self.redirect_uri
if authorization_code:
data['code'] = authorization_code
if refresh_token:
@ -48,6 +50,8 @@ class SimpleOAuthAuthenticator(object):
data=data
)
if response.status_code != 200:
print("error retrieving refresh tokens %s" % response.status_code)
print(response.content)
return None, None
refresh_token = json.loads(response.content)['refresh_token']
access_token = json.loads(response.content)['access_token']
@ -82,9 +86,10 @@ class SimpleOAuthAuthenticator(object):
except OSError:
continue
break
self.redirect_uri = "http://localhost:%s/consumer/exchange/" % port
authorize_url = (
"/o/authorize?client_id=%s&state=random_state_string&response_type=code&"
"redirect_uri=http://localhost:%s/consumer/exchange/" % (self.client_id, port)
"redirect_uri=%s" % (self.client_id, self.redirect_uri)
)
if register:
authorize_url = "%s/accounts/register/?next=%s" % (self.server_url, urlquote(authorize_url))

View File

@ -386,6 +386,10 @@ class VIEW3D_PT_blenderkit_model_properties(Panel):
layout.operator('object.blenderkit_bring_to_scene', text='Bring to scene')
# layout.operator('object.blenderkit_color_corrector')
def draw_login_progress(layout):
layout.label(text='Login through browser')
layout.label(text='in progress.')
layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL')
class VIEW3D_PT_blenderkit_profile(Panel):
bl_category = "BlenderKit"
@ -405,9 +409,7 @@ class VIEW3D_PT_blenderkit_profile(Panel):
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.login_attempt:
layout.label(text='Login through browser')
layout.label(text='in progress.')
layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL')
draw_login_progress(layout)
return
if user_preferences.enable_oauth:
@ -548,9 +550,7 @@ def draw_login_buttons(layout):
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.login_attempt:
layout.label(text='Login through browser')
layout.label(text='in progress.')
layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL')
draw_login_progress(layout)
else:
if user_preferences.api_key == '':
layout.operator("wm.blenderkit_login", text="Login",
@ -596,9 +596,7 @@ class VIEW3D_PT_blenderkit_unified(Panel):
w = context.region.width
if user_preferences.login_attempt:
layout.label(text='Login through browser')
layout.label(text='in progress.')
layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL')
draw_login_progress(layout)
return
if len(user_preferences.api_key) < 20 and user_preferences.asset_counter > 20:

View File

@ -263,7 +263,7 @@ def get_brush_props(context):
def p(text, text1='', text2='', text3='', text4='', text5=''):
'''debug printing depending on blender's debug value'''
if bpy.app.debug_value > 0:
if bpy.app.debug_value != 0:
print(text, text1, text2, text3, text4, text5)