Amazon Ad

Thursday, 30 January 2014

Authorise Your User From StackOverFlow

Hi Guys!!,

Today i am going to tell you how to validate your user from StackOverFlow API, After tonns of effort i have developed this code.
You can use this code to authenticate your user from Stackoverflow and it also give you the relevant user details.But you need to create an app
in stackexchange.com website which will provide you a clientid and key.

Here is the code





<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>StackoverFlow</title>
</head>

<body>

<script type='text/javascript' src='https://api.stackexchange.com/js/2.0/all.js'></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script type='text/javascript'>

    SE.init({
    clientId: XXXX, //Your client id from stackexchange(stackexchange.com) app.
    key: 'XXXXXXXXXXXXXXXXXX', //Your key from stackexchange(stackexchange.com) app.
    channelUrl: 'http://yoursite.com/blank', //Your website from stackexchange(stackexchange.com) app and an existing page in your website (here blank).
    complete: function (data) {
    //authenticate();
    }
});

function authenticate() {
    SE.authenticate({
        success: function (data) {

        if ($('#txtUserId').val() == '')
                $.get('https://api.stackexchange.com/2.1/users/' + data.networkUsers[0].user_id + '?site=stackoverflow', '', function success(data) { getHTML(data); }, 'json');
            else
                $.get('https://api.stackexchange.com/2.1/users/' + $('#txtUserId').val() + '?site=stackoverflow', '', function success(data) { getHTML(data); }, 'json');

                       
               
        },
        error: function (data) {
            alert('An error occurred:\n' + data.errorName + '\n' + data.errorMessage);
        },
        networkUsers: true
    });
}

function getHTML(data)
{
    $('#result').html('Loading...');
    if(data.items.length>0)
    {
        var html='';
            html+='<table border="1">'
                html+="<tr>";
                    html+="<td>User Id</td>";
                    html+="<td><b>"+data.items[0].user_id+"</b></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Name</td>";
                    html+="<td><b>"+data.items[0].display_name+"</b></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Location</td>";
                    html+="<td><b>"+data.items[0].location+"</b></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Profile Image</td>";
                    html+="<td><img src='"+data.items[0].profile_image+"'/></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Reputation</td>";
                    html+="<td><b>"+data.items[0].reputation+"</b></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Badges</td>";
                    html+="<td><b>Bronze:"+data.items[0].badge_counts.bronze+"</b> <b>Silver:"+data.items[0].badge_counts.silver+"</b> <b>Gold:"+data.items[0].badge_counts.gold+"</b></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Stackoverflow Link</td>";
                    html+="<td><a href='"+data.items[0].link+"'>My Link</a></td>";
                html+="<tr>";
                html+="<tr>";
                    html+="<td>Website Url</td>";
                    html+="<td><a href='"+data.items[0].website_url+"'>My Link</a></td>";
                html+="<tr>";
            html+='</table>';
           
            $('#result').html(html);
           

    }
    else
    {
        $('#result').html('');
        $('#txtUserId').val('');
    }
}

</script>




Enter User Id <input type="text" id="txtUserId"/>
<input type="button" onclick='authenticate()' value='Check Now'/>
<img  onclick='authenticate()' style='cursor:pointer' src='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojw6Iys/RD84QzQ5OjQBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3LDc3Nzc3NDcrODQ3NzQ0NCw0LjM0Nzc0Kzc0NDcsNzQsNDc0NCw0NC4sLP/AABEIACAAIAMBEQACEQEDEQH/xAAZAAADAQEBAAAAAAAAAAAAAAACBQYHAwH/xAAqEAABAwMDAgQHAAAAAAAAAAABAgMEAAUREhQhMUETccHRBiJCUVaRk//EABkBAAIDAQAAAAAAAAAAAAAAAAAEAQIFA//EACERAAICAQMFAQAAAAAAAAAAAAABAgMRBBJBBSExUYGh/9oADAMBAAIRAxEAPwDcaABVnBx1qH4eAE0G4rn7uFKSGZSNQ0juD3FZ1OpdznTLtJDt2nVKjbHvFgfCDxctOhZJU04pJz+/WrdNnupw+GX6nBRuyuUPq0DPJ+4Jmuyd5Z5yHCkaVRioEHHrSFysct9UvgzW4Y2WL6LH5i5s1l1hlUe7MnCmVDhxPfms+5ytsVkFixeV7HadtcHXN5g/weWK3PQFSlOFOh5zWlCfprS0lE6t27l5FNVfG7bjhYPbhcnIt0jREtBaXmnFnGdXyjOB506/ApyTEh23FRWq23KG511Nj3pKdUPTQypSO0K5GKw7NbdXKKFttJ3DeFJCjzzk/ar0QW7LeSts+2C2HSmjgS3xPKZh323uyHiy34DyfECSSkkYBwKkBNu4n5fO/i570EASJ0TYqjovD1wddfaKQ40pOkA89fOgk0aoA//Z'/>
<br/><br/>
<div id="result"></div>
</body>
</html>

 

No comments:

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...