How to get username from authentication prompt popup - javascript

I have SharePoint Online classic page which has some custom JavaScript and jQuery scripts implemented and third party .NET app requiring some basic auth. When the user visits the page he is prompted to enter username and password. How can I get the username from this prompt? Prompt is standard and looks like this (picture is not mine)

You can use SharePoint's javascript API to retrieve the current user's information.
_spPageContextInfo.userId
see: https://social.technet.microsoft.com/wiki/contents/articles/29766.sharepoint-understanding-the-sppagecontextinfo-object.aspx
Another suggestion is to use the API:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getCurrentUser, "sp.js");
var currentUser;
function getCurrentUser(){
var ctx= new SP.ClientContext.get_current();
var web = ctx.get_web();
currentUser = web.get_currentUser();
ctx.load(currentUser);
ctx.executeQueryAsync(onSuccess, onFailure);
}
function onSuccess() {
alert(currentUser.get_title()); // Domain\Account
alert(currentUser.get_email());
document.getElementById('userLogin').innerHTML = currentUser.get_loginName();
}
function onFailure() {
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
<div>Currently Logged User:
<span id="userLogin"></span>
</div>
MS reference: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

If someone is still interested in this I found a way to get the user principal name with microsoft graph API https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http
https://graph.microsoft.com/v1.0/me?$select=mailnickname,onpremisesUserprincipalName was the actual request I needed

Related

How do I distribute my bookmarklet conveniently?

I just wrote my first bookmarklet. It is simple js code which takes the current page's URL and does a POST request to submit it to another page.
The problem is that I need to share this on a blogging platform (quora.com) that does not allow HTML. Thus, I need to post a link to my bookmarklet on a different website and provide a link to that. I'd like to be able to provide a single link that folks can drag to their bookmarks bar.
This is the bookmarklet code.
javascript:(function() {
var msg = window.jQuery.ajax({url:'https://www.quora.com/api/logged_in_user',async:false,type:'GET'});
var username = undefined;
if (msg) {
username = window.jQuery.parseJSON(msg.responseText.replace('while(1);',''));
window.jQuery.post('https://my-website',
{'answer_1':window.location.href,'submitter':username.name});
alert (username.name + ' nominates ' + window.location.href);
}})();

Identify online user in weemo video call application

I am working on Weemo JS API for video conference.
1) I need some technical help for identify online user for conference.
2) How can i pass data from caller to callee?
3) How online user can disconnected from cloud?
please provide some technical ref for same.
Thanks.
You can know if a user is online or not by using the weemo.getStatus('USER_UID') method.
(void) getStatus('USER_UID')
When 'USER_UID' is the value of the target user Uid (String).
You will need to use the weemo.onGetHandler(name, obj) callback to catch the answer.
Here is an example of how to get the status of a user with a 'USER_ID' equal to 'userTestStatus':
var weemo = new Weemo('AppId', 'Token', 'Type');
weemo.onGetHandler = function(name, obj) {
switch(name) {
case 'status':
var uid = obj.uid;
if(obj.value == 0) {
console.log("User "+uid+" is offline with a status "+obj.value);
} else {
console.log("User "+uid+" is online with a status "+obj.value);
}
break;
}
};
weemo.onConnectionHandler = function(message, code) {
console.log("Connection Handler : " + message + ' ' + code);
switch(message) {
case 'sipOk':
weemo.getStatus('userTestStatus');
break;
}
};
weemo.initialize();
FYI: In this example I used the getStatus in the onConnectionHandler after receiving a "sipOk" because I want to make sure that my user is completly connected before runing a getStatus. Once you user is connected to the Weemo Cloud you can execute a getStatus out of the onConnectionHandler.
Once connected you can disconnect your user by using the weemo.reset() method. This will disconnect your user from the Weemo cloud.
(void) reset()
The reset function is used in order to properly disconnect the user from the cloud, and be able to connect to the real-time platform with other credentials.
You can find more details in the documentation and sample code available on the Weemo github here.
You can also find the full Weemo JavaScript API here

Catch User 'Allow/Deny' Response on Computer Location Query

I am using MaxMind GeoIP2 JavaScript API to determine a user's location when they enter my website. This prompts the user for their permission to provide the location via the browser. I am using this information to load as the default position of a map I have on my website.
I was wondering if there is some capture for this Allow/Deny prompt click. For example, the map loads the default position if the user were to deny the request, but if there user hasn't made a choice until after the map loads, I'd like to relocate the map if they do happen to click Allow once the browser has loaded the website.
For what it is worth, here is some example code that gets the users information once permission is given (taken from the URL above):
<script type="text/javascript" src="//j.maxmind.com/js/apis/geoip2/v2.0/geoip2.js"></script>
<script type="text/javascript">
var onSuccess = function(location){
alert(
"Lookup successful:\n\n" + JSON.stringify(location, undefined, 4)
);
};
var onError = function(error){
alert(
"Error:\n\n" + JSON.stringify(error, undefined, 4)
);
};
geoip2.city(onSuccess, onError);
</script>

How would I make a post on the users wall extending this example?

I am making an app with phonegap, adobe build and using facebook for authentication with this snippet of code.
I am looking to handle the 'success' login with some form of callback I suppose, and make a post on the user's wall.
<script type="text/javascript">
var my_client_id = "133914256793487", // YOUR APP ID
my_secret = "862f10f883f8d91617b77b4b143abc8d", // YOUR APP SECRET
my_redirect_uri = "https://www.facebook.com/connect/login_success.html", // LEAVE THIS
my_type ="user_agent", my_display = "touch"; // LEAVE THIS
var facebook_token = "fbToken"; // OUR TOKEN KEEPER
var ref; //IN APP BROWSER REFERENCE
// FACEBOOK
var Facebook = {
init:function(){
// Begin Authorization
alert("we have begun");
var authorize_url = "https://www.facebook.com/dialog/oauth?";
authorize_url += "client_id=" + my_client_id;
authorize_url += "&redirect_uri=" + my_redirect_uri;
authorize_url += "&display=" + my_display;
authorize_url += "&scope=publish_stream";
//CALL IN APP BROWSER WITH THE LINK
ref = window.open(authorize_url, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event){
Facebook.facebookLocChanged(event.url);
});
} ,
facebookLocChanged:function(loc){
if (loc.indexOf("https://www.facebook.com/connect/login_success.html") >= 0 ) {
//CLOSE INAPPBROWSER AND NAVIGATE TO INDEX
ref.close();
//THIS IS MEANT TO BE DONE ON SERVER SIDE TO PROTECT CLIENT SECRET
var codeUrl = 'https://graph.facebook.com/oauth/access_token?client_id='+my_client_id+'&client_secret='+my_secret+'&redirect_uri='+my_redirect_uri+'&code='+loc.split("=")[1];
console.log('CODE_URL::' + codeUrl);
$.ajax({
url: codeUrl,
data: {},
type: 'POST',
async: false,
cache: false,
success: function(data, status){
//WE STORE THE TOKEN HERE
localStorage.setItem(facebook_token, data.split('=')[1].split('&')[0]);
},
error: function(){
alert("Unknown error Occured");
}
});
}
}
}
</script>
<script type="text/javascript">
Facebook.init();
</script>
Can anybody advise how to appropriately extend this example - and where I can find the API to be able to help myself? Currently it successfully asks the user to login - facebook pops up - I successfully accept and then it returns with Success - and a red message saying the user should retain this URL securely.
Cheers,
Andy
I know that changing the whole structure of your code is a bit of a daunting task, but I highly recommend using PhoneGap Facebook Plugin. And since you're using build, you can also easily integreate the Facebook Connect plugin from Build.
I highly recommend you check out the following two files found within the phonegap-facebook-plugin / example / HackBook folder of the PhoneGap Facebook plugin repository: index.html and js/auth.js .
When using the javascript FB Api, you can specify a callback function as the first parameter, as in FB.login(function(response) { ... }); You can find an example in the docs.
Line 25 of auth.js shows an example of making a call to the FB API, using the following code FB.api('/me' .... While this code requests information about the logged in user, you can make any javascript API call with FB.api(), which you can learn about here (look at the fourth example).
Check out the code and see how it works/how it's implemented, and let me know if you have any questions :)

Facebook Application permission

Hi I have developed a Facebook application in Flash using Action-script 3. The application is working fine. I have developed Facebook login and authentication in JavaScript. The problem is then the user is not sign in to Facebook the application works fine, provide the user login panel and application permission panel and post the desire thing on user wall but if the user is already sign in than the JavaScript wont ask for the Facebook app permission and hence the app wont post on user wall. My JavaScript code is
<script type="text/javascript">
var APP_ID = "xxxxxxxxxxxxxxx";
var REDIRECT_URI = "http://apps.facebook.com/engel-tanimiyorum/";
var PERMS = 'publish_stream , email, user_birthday'; //comma separated list of extended permissions
function init()
{
FB.init({appId:APP_ID, status: true, cookie: true, oauth: true});
FB.getLoginStatus(handleLoginStatus);
}
function handleLoginStatus(response)
{
if (response.authResponse && response.status=="connected")
{
//Show the SWF
$('#ConnectDemo').append('<h1>You need at least Flash Player 9.0 to view this page.</h1>');
swfobject.embedSWF("index.swf",
"ConnectDemo", "803", "516", "9.0", null, null, null, {name:"ConnectDemo"});
}
else
{
var params = window.location.toString().slice(window.location.toString().indexOf('?'));
top.location = 'http://graph.facebook.com/oauth/authorize?client_id='
+APP_ID
+'&scope='+PERMS
+'&redirect_uri=' + REDIRECT_URI
+ params;
}
}
$(init);
</script>
Yours quick response will be highly appreciable
Regards
I don't know if this helps or not. But instead of your
var PERMS = 'publish_stream , email, user_birthday';
I do believe it should be states as an Array and then you would list your permissions.
It works for me, so I think it should look like this:
public var PERMS:Array = ["publish_stream","email","user_birthday"];
Also, not having your variables public might be the problem too.
Also, from what I see, you don't have any buttons, or text input?
If not, you need to create those. Then have a click handler for when there is text in the text input it will activate a click handler that then will go to a submit post handler, which then goes to a get status handler that will show you the new status you have created. I may be saying this all wrong. After all, I am not using Java for my app. But it seems logical to me that that is what you would do.. Try my first suggestions out and get back to me. If you'd rather, email me at Shandan_Spencer#live.com, so I can help you some more.

Categories

Resources