Search code examples
c#microsoft-graph-api

Error calling Microsoft Graph API authentication/phoneMethods - 'Unexpected segment OpenPropertySegment. Expected property/$value'


I am trying to register a 2FA method for a user on signup for my FooBar application. I can create the user, I can update the user's information with no problems.

The API I am trying to POST to is documented here: https://learn.microsoft.com/en-us/graph/api/resources/phoneauthenticationmethod?view=graph-rest-beta

I am using this:

//Create basic JSON for the request
var jsonBuilder = new StringBuilder();
jsonBuilder.Append("{");
jsonBuilder.Append($"\"phoneType\": \"mobile\",");
jsonBuilder.Append($"\"phoneNumber\": \"{phone}\"");
jsonBuilder.Append("}");
var json = jsonBuilder.ToString();

// Get Access token
var accessToken = await GetAccessToken(settings);

// Create the URL
var http = new HttpClient();
var url = "https://graph.windows.net/[tenant guid]/users/[user id]/authentication/phoneMethods?api-version=1.6";

// Create the request
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");

//Wait for response and content to be returned
var response = await http.SendAsync(request);

The response is

{\"lang\":\"en\",\"value\":\"Unexpected segment OpenPropertySegment. Expected property/$value.\"}

Any ideas what I am doing wrong?


Solution

  • Try this

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
    var phoneAuthenticationMethod = new PhoneAuthenticationMethod
    {
        PhoneNumber = "+1 2065555554",
        PhoneType = AuthenticationPhoneType.Mobile
    };
    
    await graphClient.Me.Authentication.PhoneMethods["{phoneAuthenticationMethod-id}"]
        .Request()
        .PutAsync(phoneAuthenticationMethod);