Title: How to get access token without a user of microsoft azure using swift?
Tags: swift;azure;microsoft-graph-api
Question: I have problem with getting access token without a user of microsoft azure using swift.
My function based on https://docs.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token and like this :
let json: [String: Any] =
[
"grant_type": "client_credentials",
"client_id": myAppClientID,
"resource": "https://graph.microsoft.com",
"client_secret": myClientSecret
]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
let url = URL(string: "https://login.microsoftonline.com/" + myDirectoryID + "/oauth2/v2.0/token")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("Host", forHTTPHeaderField: "login.microsoftonline.com")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
task.resume()
But i get error : ["error": invalid_request, "error_description": AADSTS900144: The request body must contain the following parameter: 'grant_type'.
Comment: Take a look. Hopefully this will resolve your problem

Here is the accepted answer: Two things:
As stated by @md-farid-uddin-kiron, the scope is incorrect for v2 endpoint and should be https://graph.microsoft.com/.default
The body of the request should be form-data not json:
func getPostString(params:[String:Any]) -&gt; String
var data = [String]()
for(key, value) in params
data.append(key + "=\(value)")
return data.map { String($0) }.joined(separator: "&amp;")
let params: [String: Any]  = [
"client_id": myAppClientID,
"client_secret": myClientSecret,
"grant_type": "client_credentials",
"scope": "https://graph.microsoft.com/.default"
let postString = getPostString(params: params)
request.httpBody = postString.data(using: .utf8)

Here is another answer: As you are trying oauth2/v2.0/token
So you must replace below property
Incorrect for V2.0 :"resource": "https://graph.microsoft.com"
Correct for V2.0 : "scope": "https://graph.microsoft.com/.default"
See the screen shot:
For details refer this official docs
Comment for this answer: Thanks Md Farid Uddin Kiron! I got it.
Comment for this answer: Glad to hear.Happy coding!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:07/30/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<swift><azure><microsoft-graph-api>', 'Title': 'How to get access token without a user of microsoft azure using swift?', 'ViewCount': 249, 'id': 57269559, 'user_AboutMe': '', 'user_AccountId': 4924705.0, 'user_DisplayName': 'Ryan Tran', 'user_Reputation': 437.0, 'user_Views': 71.0, 'user_id': 3966282.0}}