google-api-python-client
google-api-python-client copied to clipboard
Scope Has changed error in Django Framework
Environment details
- OS type and version: 5.10.104-linuxkit
- Python version: 3.8
- pip version: pip 22.0.4
google-api-python-clientversion: Version: 2.65.0
I'm running a Django app dockerized on a Linux container with the env details listed above.
I have an endpoint to login with my google account and an enpoint to redirect to after logging in. Both of the views are pasted below to see.
These views were working correctly until i added the https://www.googleapis.com/auth/plus.business.manage scope, after adding such scope the return login view returns the following error:
Warning at /stream/return-social-account-login
Scope has changed from "https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/plus.business.manage https://www.googleapis.com/auth/userinfo.email" to "https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/business.manage https://www.googleapis.com/auth/userinfo.email".
The same client_secret.json is being used to create both instances of Flow class on each view. Honestly i don't understand why it's failing once i added the scope for business accounts. Any help is more than welcomed!
Code example
def social_account_login(request):
from google_auth_oauthlib.flow import Flow
flow = Flow.from_client_secrets_file(
os.path.join(settings.BASE_DIR, 'client_secret.json'),
scopes=[
'openid',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/plus.business.manage',
],
redirect_uri='return-login-uri...'
)
auth_uri = flow.authorization_url()
return redirect(auth_uri[0])
def return_social_account_login(request):
from google_auth_oauthlib.flow import Flow
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
import datetime
import json
flow = Flow.from_client_secrets_file(
os.path.join(settings.BASE_DIR, 'client_secret.json'),
scopes=[
'openid',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/plus.business.manage',
],
redirect_uri='return-login-uri...'
)
flow.fetch_token(code=code)
creds = flow.credentials
info = {
'token': creds.token,
'refresh_token': creds.refresh_token,
'token_uri': creds.token_uri,
'client_id': creds.client_id,
'client_secret': creds.client_secret,
'scopes': creds.scopes,
'expiry': creds.expiry.isoformat(),
}
creds = Credentials(
token=info['token'],
refresh_token=info['refresh_token'],
token_uri=info['token_uri'],
client_id=info['client_id'],
client_secret=info['client_secret'],
scopes=info['scopes'],
)
creds.expiry = datetime.datetime.fromisoformat(info['expiry'])
with build('oauth2', 'v2', credentials=creds) as user_info_service:
user_info = user_info_service.userinfo().get().execute()
print(user_info['email'])
with build('mybusinessaccountmanagement', 'v1', credentials=creds) as service:
response = service.accounts().list().execute()
return JsonResponse({'user':user_info, 'creds': info, 'business': json.dumps(response)})