Calling Graph Api from Web Api doesn't return all user data - asp.net-web-api

In my ASP.NET Core Web Api, I'm trying to call Graph API to retrieve the data of other users in the organization. I'm using the On Behalf Of flow. The bootstrap token is passed in from SPA client code.
In the Startup code, I have:
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph("https://graph.microsoft.com/beta", "user.read user.readbasic.all profile")
.AddInMemoryTokenCaches();
In the Controller's Action, I have:
_graphServiceClient.Users.Request()
.Filter($"mail eq 'someuser#myorg.com'")
.Select((user) => new {
DisplayName = user.DisplayName,
City = user.City,
Country = user.Country,
BusinessPhones = user.BusinessPhones
})
.Request()
.GetAsync();
However, at runtime, I only get the DisplayName value. I don't get the values of City, Country or BusinessPhones. They are al null.
When I tried the same query in Graph Explorer, I saw the values for all of them.
What am I missing?

First, your code snippet has an error that you wrote 2 .Request(), you should remove one.
Whatever flow you used are all generating an access token so that you can access the api, therefore when you got correct data from the api, that means you have a right setting in using the flow, so I think if there's any issue, it should locate at the part of calling api. Then I did some test in my side.
Could you pls check if you call the api directly, you can get the value of City, Country and BUsinessPhones these 3 properties? Per my test, when I call the api, I can get response like screenshot below and by default it's null
https://graph.microsoft.com/beta/users?$filter=mail eq
'tinywang#xxx.onmicrosoft.com'&$select=mail,city,country,DisplayName,BusinessPhones,userType,usageLocation
And when I followed this tutorial to call the api via sdk, I got the same result:
My idea is checking the response of calling the api first and if we can't find the issue, then could you pls provide the tutorial you followed to help reproduce the issue.

Related

Laravel - retrieve data inside controller – POST x PATCH

First important information: I’m new to laravel, so your patience is appreciated.
I’m currently migrating a framework of mine to laravel, so I’m in the early stages.
Currently, I’m trying to set up an API endpoint to make small changes on some records. I’ve already managed to set up a API for inserting records and works perfectly. However, for setting up an API for small changes (patch), I’m having difficulties, probably because I’m not fully familiar with laravel’s Request class.
My successful insert set up looks like this:
\routes\api.php
Route::post('/categories/',[ApiCategoriesInsertController::class, 'insertCategories'], function($insertCategoriesResults) {
return response()->json($insertCategoriesResults);
})->name('api.categories.insert');
\app\Http\Controllers\ApiCategoriesInsertController.php
// some code
public function insertCategories(Request $req): array
{
$this->arrCategoriesInsertParameters['_tblCategoriesIdParent'] = $req->post('id_parent');
// some code
}
With this set up, I’m able to retrieve “id_parent” data set through POST.
So, I tried to do exactly the same architecture for patch, but doesn’t seem to work:
\routes\api.php
Route::patch('/records/',[ApiRecordsPatchController::class, 'patchRecords'], function($patchRecordsResults) {
return response()->json($patchRecordsResults);
})->name('api.records.patch');
\app\Http\Controllers\ApiRecordsPatchController.php
// some code
public function patchRecords(Request $req): array
{
$this->arrRecordsPatchParameters['_strTable'] = $req->post('strTable');
// some code
}
In this case, I´m using postman (PATCH request), testing the data in the "Body tab" with key "strTable" and value "123xxx" and I´m receiving “strTable” as null.
Any idea of why this is happening or if I should use another method in the Request class?
Thanks!
You can access parameters on the Request object using one of the following methods:
$req->strTable;
// or
$req->input('strTable');
The input method also accepts a second parameter which will be used as the default return value if the key is not present in the Request.
If you want to check whether or not the Request contains a value before you attempt to access it, you can use filled:
if ($req->filled('strTable')) {
// The request contains a value
}
Turns out that the way I had set up was in fact working and retrieving data:
$req->post('strTable');
The problem was in how I was testing it. In postman, there are several options to configure:
form-data
x-www-form-urlencoded
raw
binary
I had already switched to x-www-form-urlencoded to test it, but I forgot to fill the “key” and “value” information again. I didn’t realize that the fields blank as we switch between them.
Summing it up: It works when x-www-form-urlencoded selected but doesn’t work with form-data selected. Don’t know what the difference between them yet, but I’ll research it further.
By the way, it worked also with the suggestion from Rube Hart:

Hyperledger Composer REST API for GET

If for example an Asset contains Relationships property for owner then when using GetSingle API (from a client App) we get back something like: "owner":"resource:somenamespace.owner#owner01". (i.e. related participant is owner with ownerId = owner01).
What is actually being returned for the relationship property?
Is there anyway we can actually get back all the properties of the related instance (owner with ownerId = owner01) in the same call? I have tried adding parameter to the Rest call such as below but no joy.?resolve=true&include=true.
If I can't get related instance properties in same Rest call then how do I extract out the related Id (e.g. owner01 in above example) from the returned "owner":"resource:somenamespace.owner#owner01"I can then do a separate GetSingle call to the owner Rest end point using the extracted ownerId?I have tried searching the web for docs on such Rest API parameters/processing of results but no joy.Many thanks.Lalji
1) What's returned is a URI resource ID for the related object (ie owner)
2) Yes. Use Loopback filters to resolve. For example ('GET' call - eg. using httpClient or axios etc eg - two examples:
GET 'http://localhost:3000/api/CommodityAsset?filter={"where":{"assetId":"A01"},"include":"resolve"}' // narrow filter - for one asset
GET 'http://localhost:3000/api/CommodityAsset?filter={"where":{"ownerId":"resource:org.acme.biznet.owner%23T01"},"include":"resolve"}' // broader, assets owned by a particular owner, could be many
where %23 is '#' the encoded character used in Composer language relationship notation.

How can I retrieve list members using given a unique_email_id on Mailchimp API v3?

Given a unique_email_id, how can I retrieve all list members matching that ID?
I am attempting to use the search-members endpoint, ala what I do for searching email-addresses:
https://us9.api.mailchimp.com/3.0/search-members?query=chris#chris.com
but instead (given a unique_email_id of 4dce5
https://us9.api.mailchimp.com/3.0/search-members?query=4dce5
and I'm getting no results.
Is there another endpoint I'm missing?
However using the query string ?unique_email_id=X attached to the end
of GET lists/list_id/members will pull up anyone with that specific
EUID.
Indeed this works very well. Example :
GET /3.0/lists/dfc3cf4b38/members?unique_email_id=f4b75a9d78&fields=members.email_address
will return :
{"members":[{"email_address":"my_email#hotmail.com"}]}
I've had a chat with the mailchimp support about this issue. This was there response (part of it because not everything discussed in that chat is relevant to this question):
(09:00:04 AM) Mailchimp support: Thanks for hanging in there, Me. It looks like
the email_unique_id can't be searched via API endpoint like it can be
within the app. Another option to get to your goal is GET to
lists/list_id/members with the query string
?fields=members.email_address,members.unique_email_id which should
pull up all their subscribers' emails and unique ids. At that point,
you can filter through those results for the unique id
(09:00:52 AM) Me: Same goes for the user ID I guess?
(09:02:05 AM) Me: Why can't it be searched? When I list ids as well I might miss information. I don't need the unique_email_id in my response, I need to search for it to retrieve user information without getting duplicates in my result
(09:07:11 AM) Mailchimp support: Ok I understand, one
moment while we test this out for you. Thanks again for your patience!
(09:16:22 AM) Mailchimp support: Hey Me, thanks for waiting. I tested the
endpoint lists/list_id/members?unique_email_id=e8da8fa60a and  I was
able to return just the one subscriber with that field. Search-Members
only functions the way it does in app where you can only search
specific strings like names, email addresses, but not ids as those
aren't visible in app. However using the query string
?unique_email_id=X attached to the end of GET lists/list_id/members
will pull up anyone with that specific EUID.

Writing data to model from POST request (Odoo 9.0)

I have the following model:
class LibraryBook(models.Model):
_name = 'library.book'
name = fields.Char('Title', required=True)
date_release = fields.Date("Release Date")
author_ids = fields.Many2many("res.partner", string="Authors")
I'm new to Odoo and trying to understand the basics of how to save data to my model from a POST request like the following
curl -i -X POST --data "name=Odoo%20-%20Much%20Mystery,%20Wow&author_id=Doge" http://0.0.0.0:8069/test
I found a way doing this by setting the csrf parameter in my controller to false like so:
[...]
#http.route('/test', type='http', auth='public',methods=['POST'], website=True, csrf=False)
def test(self, **kwargs):
record = request.env['library.book'].sudo()
record.create(kwargs)
I'm wondering now if there is a way to avoid setting csrf=false since I've read that it's a bad idea to do so in general. Also, what would I need to get rid of that .sudo()? Not setting csrf=false leads to a 400 BAD REQUEST with Invalid CSRF token. Removing sudo() leads to a 500 INTERNAL SERVER ERROR. In Odoo Development Cookbook it says in one example with auth='none'
Lack of a user is also why we have to sudo() all our calls to model methods in the example code
Assuming I would expect a POST request from an API, is it possible to associate it with a user so I don't have to sudo()?
I would very much appreciate any clarification on this.
UPDATE
So I just found this (line 817):
if the form is accessed by an external third party (e.g. REST API endpoint, payment gateway callback) you will need to disable CSRF
protection (and implement your own protection if necessary) by
passing the csrf=False parameter to the route decorator.
which I guess leaves only one question open, regarding sudo.
SUDO()
creates a new environment with the provided user set, uses the administrator if none is provided (to bypass access rights/rules in safe contexts), returns a copy of the recordset it is called on using the new environment:
Odoo does not allow public users to create, update, delete a record.
If we want to create a record from the public users then we need to create a record with the sudo().
Create record object as administrator
request.env['library.book'].sudo().create(vals)
I hope this may help you.
for more information you can navigate to following links :
https://www.odoo.com/documentation/9.0/reference/orm.html
Thanks

Returning custom contact fields via the Google contacts api

I have been unable to return any UserDefined/custom fields for a contact. I'm not even wanting to create or update an existing entry as per Programatically adding "New Custom Field" To Google Contacts using Google contact API
I have followed the guide here https://developers.google.com/google-apps/contacts/v3/reference, but still to no avail.
I've called both
https://www.google.com/m8/feeds/contacts/default/full?alt=json
and
https://www.google.com/m8/feeds/contacts/default/full/DEADBEEF?alt=json
in an attempt to get ALL of the values returned. I thought I might have to call each contact id specifically to get the full details, but that doesn't work either.
DEADBEEF is clearly replaced with a valid contact id in my scenario.
/contacts/default determines that request should return contacts for the current auth'd user, which is myself in this case.
Has anyone managed to return custom values, is this even possible?
Thanks
I found the answer, add the ?v=3.0 to the url string eg
https://www.google.com/m8/feeds/contacts/default/full/DEADBEEF?alt=json&v=3.0
However, you should use the preferred method of adding a GData-Version header.

Resources