GearPlug / microsoftgraph-python

Microsoft Graph API wrapper written in Python
MIT License
96 stars 40 forks source link

token_required decorator doesn't check office365_token #2

Closed cybfox77 closed 6 years ago

cybfox77 commented 6 years ago

When using the client with office365=True, the decorator says you require a token even if it is set.

this solved it for me:

def token_required(func):
    @wraps(func)
    def helper(*args, **kwargs):
        client = args[0]
        if not (client.token or client.office365_token):
                raise TokenRequired('You must set the Token.')
        return func(*args, **kwargs)

    return helper
yagss commented 6 years ago

Hi there, look, for the moment we just have methods for microsoft graph API, what are you trying to do exactly?

cybfox77 commented 6 years ago

Hello, I'm trying to connect to an office365 account. Given the documentation on README.md, I did the following and got it to work:

from microsoftgraph.client import Client
client=(self, CLIENT_ID, CLIENT_SECRET, office365=True)
url = client.authorization_url(redirect_uri, SCOPES, state=None)
# use the url in a button to let user start authentication with Microsoft account

def gettoken(request):
    auth_code= request.GET['code']
    token = client.exchange_code(redirect_uri, auth_code)
    # extract refresh token and expires, so I can refresh the clients' token...
    client.set_token(token)
    return redirect('/home/')

def home(request):
    # ...
    me = client.get_me()
    # ....

notice the office365=True in the client init. Then, the token is stored in client.office365_token. That's why I adapted the token_required decorator like above.

But I wouldn't mind an example in the repo on how to use the client correctly, handling of token refresh for example.

ingmferrer commented 6 years ago

Hello there,

I'm sorry that our README was not clear about the support for office 365. We intended to add support for office 365 but we ran into a lot of limitations on what we actually wanted to do. Given this we dropped our efforts to implement support for office 365 for the moment, but we already had the token management mechanics in place so we didn't remove those. For the time being we are not interested in developing anything related to the office 365 suite other than OneNote, and we are developing it using the Microsoft Graph API, not the office 365 API, but we welcome any pull request with functionality of either API.

As I said previusly, we don't have any office 365 API support, so you're actually hitting the Microsoft Graph API with the client.get_me().

Someone of our team applied a fix to your token issue, and corrected the README so you can get your token following those steps, but that's all you can do with the office 365 API at the moment.

cybfox77 commented 6 years ago

Hi, thanks for the response ! I think I misunderstood the "office365" parameter. I thought it was to indicate the kind of microsoft account the client interacts with. I'm okay with using Graph API, so everything works with office365=False, even though the account is an office365 account. Thanks for the help.

yagss commented 6 years ago

Thanks to you, and we are sorry for our mistake in the README document. If you have another issue don't hesitate to ask. Regards