错误400-错误地请求使用Microsoft Graph创建用户

时间:2019-02-12 10:37:05

标签: c# .net microsoft-graph azure-ad-graph-api

我正尝试在Microsoft文档的帮助下使用Microsoft Graph(v1.0)在租户中创建新用户。
创建用户时,总是会收到一个error 400 bad request作为响应。

我正在使用HttpClient发出帖子请求。

我的功能:

private async Task<string> BuildUser(string token, string query)
    {
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        UserCreation uc = new UserCreation
        {
            accountEnabled = this.checkBoxActive.Checked,
            displayName = this.textBoxDN.Text,
            mailNickName = this.textBoxMail.Text,
            passwordProfile = new PasswordProfile { forceChangePasswordNextSignIn = this.checkBoxChangeMDP.Checked, password = this.textBoxPassword.Text},
            userPrincipalName = this.textBoxUPN.Text
        };

        string json = JsonConvert.SerializeObject(uc);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        HttpResponseMessage response = httpClient.PostAsync(query, content).Result;
        return response.ToString();
    }

我的令牌有效,我可以进行简单的Get请求,我的应用程序具有提到的here授权。

例如,我的json变量可以包含:

{
    "accountEnabled":true,
    "displayName":"cyril testgraphh",
    "mailNickName":"cyriltestgraphh",
    "userPrincipalName":"cyriltestgraphh@mytenant.fr",
    "passwordProfile":{
        "forceChangePasswordNextSignIn":true,
        "password":"XXX"
    }
}

编辑: 我通过使用Microsoft Graph对象(Microsoft.Graph.User和Microsoft.Graph.PasswordProfile)解决了我的问题,并将.onmicrosoft.com添加到了upn

1 个答案:

答案 0 :(得分:3)

您应该检查您的代码。我尝试了以下代码,效果很好。

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

def create_model():
    model = tf.keras.models.Sequential([
      tf.keras.layers.Flatten(input_shape=(28, 28)),
      tf.keras.layers.Dense(512, activation=tf.nn.relu),
      tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(10, activation=tf.nn.softmax)
    ])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model    

epoch = 3

print('GPU:')
with tf.device('/gpu:0'):   
    model = create_model()

    model.fit(x_train, y_train, epochs=epoch)

print('\nCPU:')
with tf.device('/cpu:0'):   
    model = create_model()

    model.fit(x_train, y_train, epochs=epoch)

响应如下:

enter image description here