Seite 1 von 1

AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Samstag 4. März 2017, 14:12
von kleiner_syrer
Hallo

habe folgenden Code:

Code: Alles auswählen

#!C:\Python\python.exe
# -*- coding: utf-8 -*-

from __future__ import print_function
import sys
import subprocess
import os
import httplib2
import smtplib
import active_directory
import logging
import codecs

# KTT apiclient change into googleapiclient'''
from googleapiclient import discovery
from oauth2client import client
from oauth2client.file import Storage
from json import dumps
from email.mime.text import MIMEText


# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/admin-directory_v1-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
CLIENT_SECRET_FILE = 'C:\\Python\\Ownscripts\\client_secret.json'
APPLICATION_NAME = 'Directory API Python Quickstart'


# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = codecs.open('C:\\Python\\Ownscripts\\textfile.txt', 'r')
msg_replace = fp.read()
msg_replace = msg_replace.replace("_Benutzerkennung_", sys.argv[1])
msg_replace = msg_replace.replace("_Anleger-UID_", sys.argv[3])
# Create a text/plain message
msg = MIMEText(msg_replace)
fp.close()


# Mail Konstanten
user = active_directory.find_user(str(sys.argv[1]))
CC_mail = active_directory.find_user(str(sys.argv[3]))


sender = mail@web.de
mailuser = mail1@web.de
msg['Subject'] = 'Google Account wurde angelegt.'
msg['From'] = sender
msg['To'] = mailuser
s = smtplib.SMTP('Server03')

ProxyInfo = httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP, proxy_host='Server01', proxy_port=8080,
                               proxy_user='$NWVM-PROXY', proxy_pass='Passwort')


def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'admin-directory_v1-python-quickstart.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        print('Storing credentials to ' + credential_path)
    return credentials


def main():

    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http(proxy_info=ProxyInfo))
    discovery.build('admin', 'directory_v1', http=http)
    http.request(
        uri='https://www.googleapis.com/admin/directory/v1/users',
        method='POST',
        headers={'Content-Type': 'application/json; charset=UTF-8'},
        body=dumps({"kind": "admin#directory#user",
                    "password": str(sys.argv[2]),
                    "changePasswordAtNextLogin": True,
                    "orgUnitPath": '''/''' + user.userPrincipalName.split("@", 1)[-1],
                    "primaryEmail": user.userPrincipalName,
                    "customerID": "my_customer",
                    "name": {"givenName": user.givenName, "familyName": user.sn}}
                   ))
    subprocess.call(
        'runas.exe /user:XX-DD\$NWVMAD-USER /savecred "C:\Python\OwnScripts\ADUserGroup.cmd "' + str(sys.argv[1]))
    s.sendmail(sender, [mailuser], msg.as_string('UTF-8'))
    s.quit()



if __name__ == '__main__':
    main()
Erhalte beim Ausführen auf dem Server folgende Nachricht:

Traceback (most recent call last):
File "C:\Python\Ownscripts\quickstart_ent.py", line 120, in <module>
main()
File "C:\Python\Ownscripts\quickstart_ent.py", line 95, in main
http = credentials.authorize(httplib2.Http(proxy_info=ProxyInfo))
AttributeError: 'NoneType' object has no attribute 'authorize'

Anscheinend kommt aus def get_credentials(): keine Inhalt zurück. Warum?

Danke für Eure Antwort

Gruß

Frank

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Samstag 4. März 2017, 14:26
von snafu
Lass dir mal die Rückgabe von store.get() ausgeben und überleg dann mal, ob damit ein Zusammenhang bestehen könnte...

Übrigens ist es hilfreich, den Originalcode zu posten oder bei geändertem Code diesen nochmal auszuführen und dessen Fehlermeldung zu kopieren. Dann stimmen die Zeilennummern auch überein. ;)

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Samstag 4. März 2017, 17:14
von kleiner_syrer
Das habe ich auch schon. Es wird none zurück gegeben. Mit Storage(credential_path) sollte ja alles in die admin-directory_v1-python-quickstart.json geschrieben werden. Dass passiert aber nicht. Die Datei wird gar nicht erst angelegt. Credential_path stimmt. Da wird der Pfad und die Datei angezeigt. Es muss also am Storage(credential_path) liegen. Storage hängt wiederum mit oauth2client zusammen. Aber genau da weiß ich nicht mehr weiter.

Berechtigungsprbleme sollten es nicht sein, weil das Verzeichnis .credentials wird immer abgelegt, wenn es nicht existiert.

Das mit dem Code entschuldige bitte.

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Samstag 4. März 2017, 17:35
von BlackJack
@kleiner_syrer: Wo denkst Du denn das die Datei geschrieben werden sollte? Kann es sein das der Code dafür eingentlich an der Stelle stehen sollte wo das `print()` in `get_credentials()` *behauptet* es würden „credentials“ gespeichert? Warum wird mit `flow` nichts gemacht was irgendeinen weitergehenden Effekt hat?

Wenn `credentials` `None` oder nicht gültig ist, wird zwar etwas gemacht, aber es wird auch nicht dafür gesorgt dass das am Ende der Funktion an gültige `credentials` gebunden wird.

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Montag 6. März 2017, 07:43
von kleiner_syrer
So jetzt wird es ein wenig kurios. Auf dem Client Windows 10 und Python 2.7.13 funktioniert es. Auf Windows 2008 mit Python 2.7.13 nicht. Woran kann das liegen? Alle Module sind gleich. Eine Idee?

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Montag 6. März 2017, 09:55
von BlackJack
@kleiner_syrer: Auf dem Rechner wo es funktioniert gibt es die Daten zur Autorisierung und bei dem anderen nicht‽

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Montag 6. März 2017, 16:46
von kleiner_syrer
Jo, auf beiden gibt es die Client_Secret.json. Auf dem Server wird die admin-directory_v1-python-quickstart.json.json nicht erzeugt auf dem Client schon.

Re: AttributeError: 'NoneType' object has no attribute 'authorize'

Verfasst: Montag 6. März 2017, 17:07
von snafu
Vielleicht fehlen auf dem Server die benötigten Schreibrechte für den Dateipfad. Sowas geht normalerweise aus den Logfiles hervor.