Getting User based bearer token

%3CLINGO-SUB%20id%3D%22lingo-sub-1564275%22%20slang%3D%22en-US%22%3EGetting%20User%20based%20bearer%20token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1564275%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20can%20i%20create%20a%20user%20based%20bearer%20token%20using%20username%20of%20the%20user.%3C%2FP%3E%3CP%3EI%20created%20it%20following%20way.%20But%20for%20that%20it%20needs%20user%20password.%20i%20need%20to%20do%20it%20using%20only%20user%20name.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3Eprivate%20string%20GetUserAccessToken()%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20clientId%20%3D%20%22XXXX%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20appKey%20%3D%20%22XXXX%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20tenantId%20%3D%20%22XXXX%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20accessToken%20%3D%20string.Empty%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20apiEndpoint%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%3C%2FA%3E%22%20%2B%20tenantId%20%2B%20%22%2Foauth2%2Fv2.0%2Ftoken%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20WebRequest%20accessTokenRequest%20%3D%20WebRequest.Create(apiEndpoint)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20accessTokenRequest.Method%20%3D%20%22POST%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20accessTokenRequest.ContentType%20%3D%20%22application%2Fx-www-form-urlencoded%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20requestParams%20%3D%20%22grant_type%3Dpassword%26amp%3Bclient_id%3D%22%20%2B%20clientId%20%2B%20%22%26amp%3Bclient_secret%3D%22%20%2B%20appKey%20%2B%20%22%26amp%3Bscope%3D%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2F.default%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2F.default%3C%2FA%3E%22%20%2B%20%22%26amp%3BuserName%3DXXXX%26amp%3Bpassword%3DXXXX%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20byte%5B%5D%20byteArray%20%3D%20Encoding.UTF8.GetBytes(requestParams)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20accessTokenRequest.ContentLength%20%3D%20byteArray.Length%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Stream%20dataStream%20%3D%20accessTokenRequest.GetRequestStream()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20dataStream.Write(byteArray%2C%200%2C%20byteArray.Length)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20dataStream.Close()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20using%20(WebResponse%20response%20%3D%20accessTokenRequest.GetResponse())%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20string%20json%20%3D%20string.Empty%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20using%20(StreamReader%20reader%20%3D%20new%20StreamReader(response.GetResponseStream()))%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20json%20%3D%20reader.ReadToEnd()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20O365AccessTokenModel%20accessTokenModel%20%3D%20JsonConvert.DeserializeObject%3CO365ACCESSTOKENMODEL%3E(json)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20accessToken%20%3D%20accessTokenModel.access_token%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20return%20accessToken%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%7D%3C%2FO365ACCESSTOKENMODEL%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20i%20need%20to%20do%20it%20without%20password.%20is%20there%20ant%20possible%20way%20to%20do%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1564275%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Etokens%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1565958%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20User%20based%20bearer%20token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1565958%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F738563%22%20target%3D%22_blank%22%3E%40shakila_jayarathne%3C%2FA%3E%26nbsp%3Byou%20can't%2C%20the%20OAuth%20model%20doesn't%20allow%20you%20to%20interact%20with%20the%20API%20without%20providing%20valid%20credentials.%20A%20client%20ID%20and%20secret%20would%20be%20used%20for%20an%20app%20connecting%20to%20the%20service%20on%20behalf%20of%20the%20browsing%20user.%20Can%20I%20ask%20what%20you%20are%20trying%20to%20achieve%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1569768%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20User%20based%20bearer%20token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1569768%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F749425%22%20target%3D%22_blank%22%3E%40MrCoups%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20call%20this%20%22%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2FonlineMeetings%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2FonlineMeetings%3C%2FA%3E%3C%2FSPAN%3E%22%20api.%20To%20call%20this%2C%20i%20need%20user%20based%20bearer%20token.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20reference%20%22%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fapplication-post-onlinemeetings%3Fview%3Dgraph-rest-1.0%26amp%3Btabs%3Dhttp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fapplication-post-onlinemeetings%3Fview%3Dgraph-rest-1.0%26amp%3Btabs%3Dhttp%3C%2FA%3E%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20what%20i%20try%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eprivate%20string%20AddOnlineMeeting(string%20accessToken%2Cstring%20meetingName)%3CBR%20%2F%3E%7B%3C%2FP%3E%3CP%3Estring%20webUrl%20%3D%20string.Empty%3B%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evar%20data%20%3D%20new%3CBR%20%2F%3E%7B%3CBR%20%2F%3EstartDateTime%20%3D%20%222020-07-13T14%3A30%3A34.2444915-07%3A00%22%2C%3CBR%20%2F%3EendDateTime%20%3D%20%222020-07-14T14%3A30%3A34.2444915-07%3A00%22%2C%3CBR%20%2F%3Esubject%20%3D%20meetingName%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3Evar%20serializer%20%3D%20new%20JavaScriptSerializer()%3B%3CBR%20%2F%3Evar%20json%20%3D%20serializer.Serialize(data)%3B%3C%2FP%3E%3CP%3EWebRequest%20createGroupRequest%20%3D%20WebRequest.Create(%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2FonlineMeetings%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2FonlineMeetings%3C%2FA%3E%22)%3B%3C%2FP%3E%3CP%3EcreateGroupRequest.Method%20%3D%20%22POST%22%3B%3CBR%20%2F%3EcreateGroupRequest.ContentType%20%3D%20%22application%2Fjson%22%3B%3CBR%20%2F%3EcreateGroupRequest.Headers.Add(%22Authorization%22%2C%20%22Bearer%20%22%20%2B%20accessToken)%3B%3CBR%20%2F%3Ebyte%5B%5D%20byteArray%20%3D%20Encoding.UTF8.GetBytes(json)%3B%3CBR%20%2F%3EcreateGroupRequest.ContentLength%20%3D%20byteArray.Length%3B%3CBR%20%2F%3EStream%20dataStream%20%3D%20createGroupRequest.GetRequestStream()%3B%3CBR%20%2F%3EdataStream.Write(byteArray%2C%200%2C%20byteArray.Length)%3B%3CBR%20%2F%3EdataStream.Close()%3B%3C%2FP%3E%3CP%3Eusing%20(WebResponse%20response%20%3D%20createGroupRequest.GetResponse())%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estring%20jsonR%20%3D%20string.Empty%3B%3CBR%20%2F%3Eusing%20(StreamReader%20reader%20%3D%20new%20StreamReader(response.GetResponseStream()))%3CBR%20%2F%3E%7B%3CBR%20%2F%3EjsonR%20%3D%20reader.ReadToEnd()%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3ECreateMeetingResponse%20meetingRes%20%3D%20JsonConvert.DeserializeObject%3CCREATEMEETINGRESPONSE%3E(jsonR)%3B%3CBR%20%2F%3EwebUrl%20%3D%20meetingRes.joinWebUrl%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch(Exception%20ex)%3CBR%20%2F%3E%7B%3C%2FCREATEMEETINGRESPONSE%3E%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3Ereturn%20webUrl%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1570484%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20User%20based%20bearer%20token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1570484%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F738563%22%20target%3D%22_blank%22%3E%40shakila_jayarathne%3C%2FA%3E%26nbsp%3Bhave%20you%20looked%20at%20the%20following%20page%3F%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fauth-v2-user%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fauth-v2-user%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20explains%20the%20authentication%20flow%20for%20you%20and%20the%20calls%20you%20need%20to%20make%20to%20obtain%20a%20token.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

How can i create a user based bearer token using username of the user.

I created it following way. But for that it needs user password. i need to do it using only user name.

 

           

private string GetUserAccessToken()
        {
            string clientId = "XXXX";
            string appKey = "XXXX";
            string tenantId = "XXXX";

            string accessToken = string.Empty;
            string apiEndpoint = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
            WebRequest accessTokenRequest = WebRequest.Create(apiEndpoint);
            accessTokenRequest.Method = "POST";
            accessTokenRequest.ContentType = "application/x-www-form-urlencoded";

            string requestParams = "grant_type=password&client_id=" + clientId + "&client_secret=" + appKey + "&scope=https://graph.microsoft.com/.default" + "&userName=XXXX&password=XXXX";
            byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);
            accessTokenRequest.ContentLength = byteArray.Length;
            Stream dataStream = accessTokenRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            using (WebResponse response = accessTokenRequest.GetResponse())
            {
                string json = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    json = reader.ReadToEnd();
                }
                O365AccessTokenModel accessTokenModel = JsonConvert.DeserializeObject<O365AccessTokenModel>(json);
                accessToken = accessTokenModel.access_token;
            }
            return accessToken;
        }

 

But i need to do it without password. is there ant possible way to do it.

3 Replies

@shakila_jayarathne you can't, the OAuth model doesn't allow you to interact with the API without providing valid credentials. A client ID and secret would be used for an app connecting to the service on behalf of the browsing user. Can I ask what you are trying to achieve?

@MrCoups 

 

I'm trying to call this "https://graph.microsoft.com/v1.0/me/onlineMeetings" api. To call this, i need user based bearer token.

 

Here is reference "https://docs.microsoft.com/en-us/graph/api/application-post-onlinemeetings?view=graph-rest-1.0&tabs=..."

 

Here is what i try

 

private string AddOnlineMeeting(string accessToken,string meetingName)
{

string webUrl = string.Empty;
try
{
var data = new
{
startDateTime = "2020-07-13T14:30:34.2444915-07:00",
endDateTime = "2020-07-14T14:30:34.2444915-07:00",
subject = meetingName
};

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);

WebRequest createGroupRequest = WebRequest.Create("https://graph.microsoft.com/v1.0/me/onlineMeetings");

createGroupRequest.Method = "POST";
createGroupRequest.ContentType = "application/json";
createGroupRequest.Headers.Add("Authorization", "Bearer " + accessToken);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
createGroupRequest.ContentLength = byteArray.Length;
Stream dataStream = createGroupRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

using (WebResponse response = createGroupRequest.GetResponse())
{
string jsonR = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
jsonR = reader.ReadToEnd();
}

CreateMeetingResponse meetingRes = JsonConvert.DeserializeObject<CreateMeetingResponse>(jsonR);
webUrl = meetingRes.joinWebUrl;
}
}
catch(Exception ex)
{

}

return webUrl;
}

@shakila_jayarathne have you looked at the following page?https://docs.microsoft.com/en-us/graph/auth-v2-user

 

This explains the authentication flow for you and the calls you need to make to obtain a token.