-1

I am a boiler plate programmer, so not very technical on how scripts work indepthly.

I have a yammer api for pulling user information from Yammer:

<!DOCTYPE html>
<html>
  <head>  
  </head> 
<body>

<span id="yammer-login"></span>

<script type="text/javascript" data-app-id="bdlZbJHCm1RY8pMUbuoBlQ" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>

<script>
yam.getLoginStatus(
  function (response) {
      var result = 1625803434;

    if (response.authResponse) {
      console.log("logged in");
      yam.platform.request({
        url: "users/"+result+".json",     
        method: "GET",
        data: {    //use the data object literal to specify parameters, as documented in the REST API section of this developer site
          "User_Id": result,
        },
        success: function (user) { //print message response information to the console
        str = JSON.stringify(user, null, 4); // (Optional) beautiful indented output.
        document.write(str);
        },
        error: function (user) {
          alert("There was an error with the request.");
        }
      });
    }
    else {
      alert("not logged in")
    }
  }

);

</script>

</body>
</html>

I have to hard code the Yammer id into it using the result variable.

I tried adding an input box then the yam.getLoginStatus fails with: alert("There was an error with the request.");

<!DOCTYPE html>
<html>
  <head>  
  </head> 
<body>

<form id="frm1" action="#" method="post">
  Yammer ID: <input type="text" name="y_id"><br><br>
  <input type="submit" onclick="yam_id()" value="Submit">
</form> 

<span id="yammer-login"></span>

<script type="text/javascript" data-app-id="bdlZbJHCm1RY8pMUbuoBlQ" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script>

<script>
yam.getLoginStatus(
  function (response) {
      var result = 1625803434;
    frm_i=document.getElementById("frm1");
    result=frm_i.elements[0].value;
//    alert (result);

    if (response.authResponse) {
      console.log("logged in");
      yam.platform.request({
        url: "users/"+result+".json",     
        method: "GET",
        data: {    //use the data object literal to specify parameters, as documented in the REST API section of this developer site
          "User_Id": result,
        },
        success: function (user) { //print message response information to the console
        str = JSON.stringify(user, null, 4); // (Optional) beautiful indented output.
        document.write(str);
          //console.dir(user);
        },
        error: function (user) {
          alert("There was an error with the request.");
        }
      });
    }
    else {
      alert("not logged in")
    }
  }

);
</script>

Does anyone know how to simply add an input box for this script?

hrhsii
  • 157
  • 1
  • 12

1 Answers1

0

Here are two examples I prepared for you.

One is form submit, second is alert prompt. In both cases you need to pass a value into an input otherwise function will fail to run.

Put your authentication into authUser() function. Run example code to see it in action.

// Your auth function
function authUser(id) {
  // Lets validate
  if (id.length === 0) {
    alert('Please enter your data, missing id');
    return false;
  }
  
  // ur ajax/xhr request here
  
  // Just console log so you can see the data
  console.log(id);
}

// Example 1

document.getElementById('login-action2').addEventListener('click', function() {
  var id = prompt("Please enter your auth credentials/id");
  
  // then pass your id to your auth function
  authUser(id);
});


// Example 2

function formSubmit() {
  var id = document.getElementById('login-action1').value;

  
  authUser(id);
  
  // Prevent form from auto submitting
  return false;
}
<button id="login-action2" type="button">Log me in</button>

<form action="form-login" onSubmit="return formSubmit();">

  <fieldset>
  <input id="login-action1" type="text" placeholder="enter auth credentials/id" value="" />
  <input type="submit"  value="Submit" />
  </fieldset>

</form>
loelsonk
  • 3,570
  • 2
  • 16
  • 23
  • Thank you for the update. I can get an input, the issue is passing the input to yam.getLoginStatus( before it runs. At the moment it runs before any input is received from the input box. – hrhsii Apr 12 '17 at 17:51
  • Have you read https://developer.yammer.com/docs/js-sdk ? You are missing this piece of code: `yam.connect.loginButton('#yammer-login', function (resp) { if (resp.authResponse) { document.getElementById('yammer-login').innerHTML = 'Welcome t Yammer!'; }` – loelsonk Apr 12 '17 at 18:10
  • Hi, I can login no problem, i just need to know how to pass my input yammer id to the yammer api script. It all runs and works ok, if i hard code the yammer id in the results variable. – hrhsii Apr 12 '17 at 18:25
  • So you need a user data that have logged in. Have you tried this code? `yam.getLoginStatus( function(response) { if (response.authResponse) { alert("logged in"); console.dir(response); //print user information to the console } else { //authResponse = false if the user is not logged in, or is logged in but hasn't authorized your app yet alert("logged out"); } } );` What is the console output for `resp` and `resp.authResponse`? Is there no `id`? – loelsonk Apr 12 '17 at 19:04
  • Another hint is for you to check `localStorage` for `id`, is there any? – loelsonk Apr 12 '17 at 20:12
  • Thanks for the response. My issue is not with logging in, or getting a user id, this all works! My problem is that when i have the yammer user id in the variable result, i do not know how to pass it to the api script at the begining of the question without it running with an empty variable. – hrhsii Apr 13 '17 at 11:52