GithubHelp home page GithubHelp logo

Unclear usage about dynamics365crm-python HOT 7 CLOSED

gearplug avatar gearplug commented on June 8, 2024 1
Unclear usage

from dynamics365crm-python.

Comments (7)

pcooperKAC avatar pcooperKAC commented on June 8, 2024

@duoi I'm having the same issue. Were you ever able to figure it out?

from dynamics365crm-python.

duoi avatar duoi commented on June 8, 2024

@pcooperKAC the answer is "yes" in quotation marks.

I didn't end up using the approach in the documentation. I was already using adal (here) so I ended up deciding to authenticate with that and just using this package as an API to interact with dynamics, rather than having this package deal with the authentication as well.

Something like this is what I ended up with:

import adal
from msrestazure.azure_active_directory import AADTokenCredentials
from dynamics365crm.client import Client


class Dynamics365Client():
    def __init__(self):
        super().__init__()

        self.resource_uri = DYNAMICS__RESOURCE_URI
        self.client_id = DYNAMICS__CLIENT_ID
        self.client_secret = DYNAMICS__CLIENT_SECRET
        self.tenant = DYNAMICS__TENANT

        self.client = self.authenticate()

    def _authenticate_client_key(self):
        authority_uri = 'https://login.microsoftonline.com/{tenant}'.format(
            tenant=self.tenant
        )
        context = adal.AuthenticationContext(
            authority=authority_uri,
            api_version=None
        )
        mgmt_token = context.acquire_token_with_client_credentials(
            resource=self.resource_uri,
            client_id=self.client_id,
            client_secret=self.client_secret
        )
        credentials = AADTokenCredentials(
            token=mgmt_token,
            client_id=self.client_id
        )

        return credentials

    def authenticate(self):
        self.client = Client(
            resource=self.resource_uri,
            client_id=self.client_id,
            client_secret=self.client_secret
        )
        self.client.set_token(
            token=self._authenticate_client_key().token.get('access_token')
        )

        return self.client

from dynamics365crm-python.

ingmferrer avatar ingmferrer commented on June 8, 2024

Hello there, sorry for the delay.

The process is called OAuth 2.0 authorization code flow. Basically, you construct an authorization url using the url_petition method. The user access to this url and authorize your app with a set of permissions (scopes). The user is redirected to your web server and you grab the authorization code and exchange it with the exchange_code method for an access token. Then you start using the provided access token to access resources on the user behalf.

I hope this help you to understand the flow.

If you need more information, let me know.

from dynamics365crm-python.

duoi avatar duoi commented on June 8, 2024

@ingmferrer right, the confusion seems to be around the purpose of the package.

For my usage, I was automatically adding customers to Dynamics365 after their registration. Which is why my approach was the way it was.

I guess the intention of this package is to just provide a separate interface for a human user of the CRM to interact with Dynamics?

from dynamics365crm-python.

gustav0 avatar gustav0 commented on June 8, 2024

Hello @duoi and @pcooperKAC

It seems you guys haven't had much intereaction with the OAuth authentication methods, so I'll try to explain how it works.

What is OAuth 2.0? To put it simple, it's a way to obtain user permissions to access something in their account. The access is limited to the service you are using, in this case Dynamics365 and to whatever the user gave you access to. A clear example of this is any application that asks you to link with Facebook, it will prompt a screen for you to give access for them to some items in your facebook account.

Now that you know what OAuth is lets see how can you go through this with the library.
First do this:

c = Client(**my _kwargs)
authorization_url = c.url_petition(redirect_uri='https://goto.mypage.com/oauth/dynamics365/callback')

Now you have the authorization_url and you can use it to ask any user permissions over their dynamics365 account. You can try and open it yourself and it should ask you to grant permissions.

Anytime someone gives you permissions you should get a request to the url you specified in the redirect_uri. In my case it was https://goto.mypage.com/oauth/dynamics365/callback.

That request should contain a code, usually via get paramenters, so the url should actually look like this: https://goto.mypage.com/oauth/dynamics365/callback?code=thisismycode12. Now you should get the code from that url in your application backend and call the next method:

c = Client(**my _kwargs)
code = 'thisismycode12'  # Get this from the url parameters
finally_my_token = c.exchange_code('https://goto.mypage.com/oauth/dynamics365/callback', code)

Now you have the token you need to use the library as the person who gave you access, but you can only do whatever the user authorized you to do.

I hope that helped.

from dynamics365crm-python.

pcooperKAC avatar pcooperKAC commented on June 8, 2024

@duoi I was able to get what I needed with the code that you pasted. Many thanks!

from dynamics365crm-python.

duoi avatar duoi commented on June 8, 2024

@gustav0 I'm familiar with Oauth, I just didn't feel this was necessary:

Now you have the authorization_url and you can use it to ask any user permissions over their dynamics365 account. You can try and open it yourself and it should ask you to grant permissions.

My approach doesn't require any users to interact at any stage. You authorize it from Azure and get going indefinitely, re-authenticating for each instantiation (so I don't even need to worry about renewing tokens).

from dynamics365crm-python.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.