Python requests library is not working, while cURL is working Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
Was Kant an Intuitionist about mathematical objects?
Printing attributes of selection in ArcPy?
Test print coming out spongy
Tannaka duality for semisimple groups
One-one communication
Can you force honesty by using the Speak with Dead and Zone of Truth spells together?
How do living politicians protect their readily obtainable signatures from misuse?
White walkers, cemeteries and wights
A `coordinate` command ignored
Relating to the President and obstruction, were Mueller's conclusions preordained?
How to change the tick of the color bar legend to black
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Did any compiler fully use 80-bit floating point?
Is CEO the "profession" with the most psychopaths?
AppleTVs create a chatty alternate WiFi network
Sally's older brother
What does it mean that physics no longer uses mechanical models to describe phenomena?
Can two people see the same photon?
How to ask rejected full-time candidates to apply to teach individual courses?
Mounting TV on a weird wall that has some material between the drywall and stud
Monty Hall Problem-Probability Paradox
Weaponising the Grasp-at-a-Distance spell
Flight departed from the gate 5 min before scheduled departure time. Refund options
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
Python requests library is not working, while cURL is working
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to retrieve a JWT (JSON Web Token) from a Microsoft API using Python (check this API documentation for Microsoft Graph)
The following Python code using the requests
library does not work giving HTTP response code 400, however, the equivalent cURL command does work giving back the expected JSON containing the JWT.
Python / requests
code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_query_params =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, params=http_query_params, headers=http_headers)
cURL command:
curl -v -X POST
--data-urlencode 'client_id=<MY_FOO_C_ID>'
--data-urlencode 'scope=<MY_FOO_SCOPE>'
--data-urlencode 'client_secret=<MY_FOO_C_SECRET>'
--data-urlencode 'grant_type=client_credentials'
-H 'Content-Type: application/x-www-form-urlencoded'
'https://login.microsoftonline.com/<MY_FOO_TENANT>/oauth2/v2.0/token'
From the verbose output of the requests
library I can see that it is URL encoding all those HTTP query parameters, so I tend to think that should not be the problem.
- what's wrong with the Python implementation?
- how to make it work?
python python-3.x python-requests jwt microsoft-graph
add a comment |
I need to retrieve a JWT (JSON Web Token) from a Microsoft API using Python (check this API documentation for Microsoft Graph)
The following Python code using the requests
library does not work giving HTTP response code 400, however, the equivalent cURL command does work giving back the expected JSON containing the JWT.
Python / requests
code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_query_params =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, params=http_query_params, headers=http_headers)
cURL command:
curl -v -X POST
--data-urlencode 'client_id=<MY_FOO_C_ID>'
--data-urlencode 'scope=<MY_FOO_SCOPE>'
--data-urlencode 'client_secret=<MY_FOO_C_SECRET>'
--data-urlencode 'grant_type=client_credentials'
-H 'Content-Type: application/x-www-form-urlencoded'
'https://login.microsoftonline.com/<MY_FOO_TENANT>/oauth2/v2.0/token'
From the verbose output of the requests
library I can see that it is URL encoding all those HTTP query parameters, so I tend to think that should not be the problem.
- what's wrong with the Python implementation?
- how to make it work?
python python-3.x python-requests jwt microsoft-graph
add a comment |
I need to retrieve a JWT (JSON Web Token) from a Microsoft API using Python (check this API documentation for Microsoft Graph)
The following Python code using the requests
library does not work giving HTTP response code 400, however, the equivalent cURL command does work giving back the expected JSON containing the JWT.
Python / requests
code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_query_params =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, params=http_query_params, headers=http_headers)
cURL command:
curl -v -X POST
--data-urlencode 'client_id=<MY_FOO_C_ID>'
--data-urlencode 'scope=<MY_FOO_SCOPE>'
--data-urlencode 'client_secret=<MY_FOO_C_SECRET>'
--data-urlencode 'grant_type=client_credentials'
-H 'Content-Type: application/x-www-form-urlencoded'
'https://login.microsoftonline.com/<MY_FOO_TENANT>/oauth2/v2.0/token'
From the verbose output of the requests
library I can see that it is URL encoding all those HTTP query parameters, so I tend to think that should not be the problem.
- what's wrong with the Python implementation?
- how to make it work?
python python-3.x python-requests jwt microsoft-graph
I need to retrieve a JWT (JSON Web Token) from a Microsoft API using Python (check this API documentation for Microsoft Graph)
The following Python code using the requests
library does not work giving HTTP response code 400, however, the equivalent cURL command does work giving back the expected JSON containing the JWT.
Python / requests
code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_query_params =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, params=http_query_params, headers=http_headers)
cURL command:
curl -v -X POST
--data-urlencode 'client_id=<MY_FOO_C_ID>'
--data-urlencode 'scope=<MY_FOO_SCOPE>'
--data-urlencode 'client_secret=<MY_FOO_C_SECRET>'
--data-urlencode 'grant_type=client_credentials'
-H 'Content-Type: application/x-www-form-urlencoded'
'https://login.microsoftonline.com/<MY_FOO_TENANT>/oauth2/v2.0/token'
From the verbose output of the requests
library I can see that it is URL encoding all those HTTP query parameters, so I tend to think that should not be the problem.
- what's wrong with the Python implementation?
- how to make it work?
python python-3.x python-requests jwt microsoft-graph
python python-3.x python-requests jwt microsoft-graph
edited Mar 22 at 14:34
Fozoro
1,8312927
1,8312927
asked Mar 22 at 11:47
TPPZTPPZ
1,03312049
1,03312049
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you should pass http_query_params
as data
instead of params
. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_body =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps
Thanks that helps and it works. However this means thathttp_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works incURL
is due to the fact that--data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP headerContent-Type: application/x-www-form-urlencoded
), right?
– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL--data-urlencode
. Your suggestion works, and looking at therequests
docs it seems thedata=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.
– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention thathttp_query_params
should be renamed tohttp_body
and (AFAIK) this probably works as HTTP body because of the HTTP headerContent-Type: application/x-www-form-urlencoded
.
– TPPZ
Mar 22 at 13:02
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298936%2fpython-requests-library-is-not-working-while-curl-is-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you should pass http_query_params
as data
instead of params
. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_body =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps
Thanks that helps and it works. However this means thathttp_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works incURL
is due to the fact that--data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP headerContent-Type: application/x-www-form-urlencoded
), right?
– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL--data-urlencode
. Your suggestion works, and looking at therequests
docs it seems thedata=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.
– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention thathttp_query_params
should be renamed tohttp_body
and (AFAIK) this probably works as HTTP body because of the HTTP headerContent-Type: application/x-www-form-urlencoded
.
– TPPZ
Mar 22 at 13:02
|
show 1 more comment
you should pass http_query_params
as data
instead of params
. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_body =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps
Thanks that helps and it works. However this means thathttp_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works incURL
is due to the fact that--data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP headerContent-Type: application/x-www-form-urlencoded
), right?
– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL--data-urlencode
. Your suggestion works, and looking at therequests
docs it seems thedata=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.
– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention thathttp_query_params
should be renamed tohttp_body
and (AFAIK) this probably works as HTTP body because of the HTTP headerContent-Type: application/x-www-form-urlencoded
.
– TPPZ
Mar 22 at 13:02
|
show 1 more comment
you should pass http_query_params
as data
instead of params
. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_body =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps
you should pass http_query_params
as data
instead of params
. try the following code:
tenant = "<MY_FOO_TENANT>"
token_url = "https://login.microsoftonline.com//oauth2/v2.0/token".format(tenant)
http_headers =
'Content-Type': 'application/x-www-form-urlencoded',
http_body =
"client_id": "<MY_FOO_C_ID>",
"scope": "<MY_FOO_SCOPE>",
"client_secret": "<MY_FOO_C_SECRET>",
"grant_type": "client_credentials",
http_response = requests.post(token_url, data=http_body, headers=http_headers)
hope this helps
edited Mar 22 at 13:05
answered Mar 22 at 12:25
FozoroFozoro
1,8312927
1,8312927
Thanks that helps and it works. However this means thathttp_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works incURL
is due to the fact that--data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP headerContent-Type: application/x-www-form-urlencoded
), right?
– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL--data-urlencode
. Your suggestion works, and looking at therequests
docs it seems thedata=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.
– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention thathttp_query_params
should be renamed tohttp_body
and (AFAIK) this probably works as HTTP body because of the HTTP headerContent-Type: application/x-www-form-urlencoded
.
– TPPZ
Mar 22 at 13:02
|
show 1 more comment
Thanks that helps and it works. However this means thathttp_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works incURL
is due to the fact that--data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP headerContent-Type: application/x-www-form-urlencoded
), right?
– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL--data-urlencode
. Your suggestion works, and looking at therequests
docs it seems thedata=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.
– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention thathttp_query_params
should be renamed tohttp_body
and (AFAIK) this probably works as HTTP body because of the HTTP headerContent-Type: application/x-www-form-urlencoded
.
– TPPZ
Mar 22 at 13:02
Thanks that helps and it works. However this means that
http_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works in cURL
is due to the fact that --data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP header Content-Type: application/x-www-form-urlencoded
), right?– TPPZ
Mar 22 at 12:54
Thanks that helps and it works. However this means that
http_query_params
is not really HTTP query params (?xxx=yyy&aaa=bbb
), but instead it's the HTTP request body, right? And the reason it works in cURL
is due to the fact that --data-urlencode
is not actually encoding HTTP query parameters, but it's URL encoding HTTP request body due to the "form header format" (HTTP header Content-Type: application/x-www-form-urlencoded
), right?– TPPZ
Mar 22 at 12:54
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
Hmmm, I see so basically you don’t want it to be in the body of the request but in the URL?
– Fozoro
Mar 22 at 12:56
I think I was tricked by that "form header" combined with my misunderstanding of cURL
--data-urlencode
. Your suggestion works, and looking at the requests
docs it seems the data=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.– TPPZ
Mar 22 at 12:59
I think I was tricked by that "form header" combined with my misunderstanding of cURL
--data-urlencode
. Your suggestion works, and looking at the requests
docs it seems the data=
parameter is for HTTP bodies. So basically the example in the Microsoft documentation is a bit misleading when presenting those key/value pairs as (URL encoded) HTTP query parameters.– TPPZ
Mar 22 at 12:59
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Oh great! So does my code solve your issue? Or you have any other problems that you are facing now?
– Fozoro
Mar 22 at 13:00
Yes, I would just add a mention that
http_query_params
should be renamed to http_body
and (AFAIK) this probably works as HTTP body because of the HTTP header Content-Type: application/x-www-form-urlencoded
.– TPPZ
Mar 22 at 13:02
Yes, I would just add a mention that
http_query_params
should be renamed to http_body
and (AFAIK) this probably works as HTTP body because of the HTTP header Content-Type: application/x-www-form-urlencoded
.– TPPZ
Mar 22 at 13:02
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298936%2fpython-requests-library-is-not-working-while-curl-is-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown