web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

WebAPI Access Token

(0) ShareShare
ReportReport
Posted on by 80

HI 

I have successfully connected D365 WebAPI with Postman but every time I GET or POST something , I need to regenerate access token and use that new token . Is there any way I use only one token and not need to recreate it ?

Second question is , How I can use WebAPI on my website to send data from my website(json , php) to D365 ? As I need to recreate access token every time , I can not use access token . Is there anyway I can use only API key or clientid and client secrete key for connection ?

Thanks

Hemisha

I have the same question (0)
  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hi,

    Wherever you create a token from the postman, it expires in a few mins. You can not use one token every time but you can refresh the token if it expires.

    Also I would suggest you to keep using Oauth2.0 i.e. client id and secret for connection as it is more secure than any other auth.

  • HGajjar Profile Picture
    80 on at

    Hi Naveen

    Yes I am also thinking of using client id etc only but can not find any documents.

    Is there any code or document for JSON or php code to implement it

    Thanks

    Hemisha

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hello,

    you can try the below PHP code for connection:

     'client_credentials',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'resource' => 'https://your_d365_instance.crm.dynamics.com'
    ];
    
    $url = "https://login.microsoftonline.com/$tenantId/oauth2/token";
    
    // Use cURL to send the token request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($curl);
    $response = json_decode($response);
    $accessToken = $response->access_token;
    
    curl_close($curl);
    
    // Use the access token to make a request to the Web API
    $headers = [
        "Authorization: Bearer $accessToken",
        "OData-MaxVersion: 4.0",
        "OData-Version: 4.0",
        "Accept: application/json",
        "Content-Type: application/json; charset=utf-8"
    ];
    
    $url = "https://your_d365_instance.crm.dynamics.com/api/data/v9.1/accounts";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    echo $response;
    

  • HGajjar Profile Picture
    80 on at

    Hi

    Thank you for your code . Now I am getting bellow error

    : {"error":{"code":"0x80072560","message":"The user is not a member of the organization."}}

    I did check all setting . User is the member of organization. Dont know what else I need to set .

    Thanks

    Hemisha

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Please check if the user has appropriate rights. Also you have an application user in D365

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,409 Moderator on at

    Check if the user have appropriate roles in Dynamics 365 environment?

  • HGajjar Profile Picture
    80 on at

    Hi

    I am the admin of the organization but still getting same error .

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Please check if the application user has the correct rights.

    docs.metallic.io/.../136228_register_dynamics_365_application_with_azure.html

    Please check point 14 in this: www.c-sharpcorner.com/.../

    monitoringguys.com/.../

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Check if you have enabled security group for your Environment, if yes then make sure your user is part of this security group

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 62 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 43

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans