Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

WebAPI Access Token

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

  • Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: WebAPI Access Token

    Good to hear that

  • HGajjar Profile Picture
    HGajjar 80 on at
    RE: WebAPI Access Token

    HI

    I have solved the issues . It was silly php syntax issues .

    'Authorization: Bearer '.$accessToken   instead of     'Authorization: Bearer $accessToken'

    Thank you all for help .

    Thanks

    Hemisha

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: WebAPI Access Token

    Hi Hemisha,

    Please take a look at these git repo. They are also using similar kind of integration: github.com/.../php-crm-toolkit

    github.com/.../dynamics-sdk-php

    You can find the code in src folder.

  • HGajjar Profile Picture
    HGajjar 80 on at
    RE: WebAPI Access Token

    Hi

    I have created new user and then it was working fine. But but when I try to create account using bellow code , I am getting error . Same thing working fine in postman .

    I am getting 

    {"error":{"code":"0x80048d19","message":"Error identified in Payload provided by the user for Entity :'accounts'

    my code :

    $curl = curl_init();
    $clientId = "xxxxxxxx";
    $clientSecret = "xxxxxxx";
    $tenantId = "xxxxxxx";

    // Build the body for the token request
    $body = [
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'resource' => 'https://xxxx.dynamics.com'
    ];

    $url = "">login.microsoftonline.com/.../token";
    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);

    $headers = [
    "Authorization: Bearer ".$accessToken,
    "OData-MaxVersion: 4.0",
    "OData-Version: 4.0",
    "Accept: application/json",
    "Content-Type: application/json; charset=utf-8"
    ];

    $body1 = [

    'name' => 'testtest'

    ];

    $url = "">xxxxxx/.../accounts";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body1));

    $response = curl_exec($curl);
    curl_close($curl);

    Any one suggest me why ?

    Thanks

    Hemisha

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: WebAPI Access Token

    Hi,

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

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: WebAPI Access Token

    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/.../

  • HGajjar Profile Picture
    HGajjar 80 on at
    RE: WebAPI Access Token

    Hi

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

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,525 Super User 2024 Season 1 on at
    RE: WebAPI Access Token

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

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: WebAPI Access Token

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

  • HGajjar Profile Picture
    HGajjar 80 on at
    RE: WebAPI Access Token

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans