Jump to content
Officeapi

Got message 'HTTP/1.1 401 Unauthorized' when tried to access user profile (https://graph.microsoft.com/v1.0/me)

Recommended Posts

so you do get a response now, try now without the Async, i think problem might be in the Async method now.  (not in the method, but how you handle it).

I never used that method, i believe you have to have an event catching the response or something like that ?

(can check that tomorrow, i now go to bed : )

 

Share this post


Link to post
4 hours ago, Officeapi said:

I tried Execute instead of ExecuteAsync, Response.JSONValue is still null.

Very strange, if you get a response code of 200 and you see the body in the proxy it should have a value, can you see if request.Response.Content contains any text?

(debug it right after request.execute in your code)


 

  • Like 1

Share this post


Link to post

Here are the values I got during debug:

 

RESTProfileResponse.Content : ''
RESTProfileResponse.ContentLength : 348
RESTProfileResponse.ContentType : 'application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false'
RESTProfileResponse.ContentEncoding : ''
RESTProfileResponse.FullRequestURI : 'https://graph.microsoft.com/v1.0/me'
RESTProfileResponse.Headers : (nil, $5A528A0, #$D#$A, nil, 0, ',', '"', '=', [soWriteBOM,soTrailingLineBreak,soUseLocale])
RESTProfileResponse.JSONText : ''
RESTProfileResponse.JSONValue : nil
RESTProfileResponse.Observers : ($120E3090, TComponent.GetObservers$ActRec($120FFAB4) as TObservers.TCanObserveEvent, TComponent.GetObservers$ActRec($120FFAB0) as TObservers.TObserverAddedEvent)
RESTProfileResponse.RootElement : ''
RESTProfileResponse.StatusCode : 200

Share this post


Link to post
27 minutes ago, Officeapi said:

Here are the values I got during debug:

 

RESTProfileResponse.Content : ''
RESTProfileResponse.ContentLength : 348
RESTProfileResponse.ContentType : 'application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false'
RESTProfileResponse.ContentEncoding : ''
RESTProfileResponse.FullRequestURI : 'https://graph.microsoft.com/v1.0/me'
RESTProfileResponse.Headers : (nil, $5A528A0, #$D#$A, nil, 0, ',', '"', '=', [soWriteBOM,soTrailingLineBreak,soUseLocale])
RESTProfileResponse.JSONText : ''
RESTProfileResponse.JSONValue : nil
RESTProfileResponse.Observers : ($120E3090, TComponent.GetObservers$ActRec($120FFAB4) as TObservers.TCanObserveEvent, TComponent.GetObservers$ActRec($120FFAB0) as TObservers.TObserverAddedEvent)
RESTProfileResponse.RootElement : ''
RESTProfileResponse.StatusCode : 200

 

ah ok.

You can see that there is content (348 bytes)
 

but i think i know the problem, probably your accept header is not ok.

more will follow in a minute, i check something.....

 

you have this RESTProfileRequest.Params.AddItem('Accept', '*/*', TRESTRequestParameterKind.pkHTTPHEADER); somewhere? 

 

change the '*/* in 'application/json'

or better just use the constant: CONTENTTYPE_APPLICATION_JSON

(is declared in the Rest.Types unit)

 

i use:

req.AddParameter('Accept', ctAPPLICATION_JSON, pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);

 

Edited by mvanrijnen

Share this post


Link to post

I don't have setting 'Accept'.

After I added req.AddParameter('Accept', ctAPPLICATION_JSON, pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);

the JSONValue is still null.

 

Just check the component, there is setting for Accept: 'application/json, text/plain; q=0.9, text/html;q=0.8,'

Edited by Officeapi

Share this post


Link to post
20 minutes ago, Officeapi said:

I don't have setting 'Accept'.

After I added req.AddParameter('Accept', ctAPPLICATION_JSON, pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);

the JSONValue is still null.

 

Just check the component, there is setting for Accept: 'application/json, text/plain; q=0.9, text/html;q=0.8,'

 

very strange. and in the proxy you still  get json text in the response?
 

Share this post


Link to post
On 11/9/2023 at 9:54 PM, Officeapi said:

Here is the info I get from Charles Proxy

The Content-Type header value looks suspicious:

 

application%2Fjson

 

 

RESTListResGroupResponse.JSONValue maybe is empty because the content type is not the expected one, application/json

 

As can be seen in the screen shot, all other slashes are not encoded as #2F.

 

If the request header Content-Type is not required, just remove it from the request setup and try if this helps.

Edited by mjustin

Share this post


Link to post

I created TRESTClient/Request in the code, and used seperate Clients. Still got JSONValue null. Is it possible that TRESTClient doesn't work in some situation?

Share this post


Link to post
On 11/13/2023 at 2:28 PM, Officeapi said:

I created TRESTClient/Request in the code, and used seperate Clients. Still got JSONValue null. Is it possible that TRESTClient doesn't work in some situation?

i'm out of idea, very strange that you get a statuscode 200, visible content in the proxy but not content in the delphi object. 

 

Share this post


Link to post

It turns out that TRESTResponse may have some bug. Remember the 'ContentLength' is 348, and 'Content' is empty? After encode the 'RawBytes' of the response, I can see the profile content. For some unknown reason, TRESTResponse ignores the content.

Thanks for all the replies.

Share this post


Link to post

Could you attach/upload the last version of your code? This would make it easier to reproduce the issue and to create a bug report for it.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×