create users in yammer group without having the user sign and achieve it through impersonation

StackOverflow https://stackoverflow.com/questions/19104677

  •  29-06-2022
  •  | 
  •  

Вопрос

I wanted to create users in yammer group without having the user sign and achieve it through impersonation.

Getting access_tokes on the behalf of other users [Getting unauthorized header in response]: I am initiating a GET request using Fiddler to get access token on the behalf of other users with following parameters

https://www.yammer.com/api/v1/oauth/tokens.json - url used

parameters user_id and consumer_key used...

enter image description here

I am always getting a unauthorized exception. can any one help me in this context. how do I impersonate through my application and create users in yammer.

Это было полезно?

Решение

You can use an Oauth token of an administrative user to look up another users token. Then with that users token, join them to the group by specifying the group name. Here's how we do it in PHP...

$ymuser = yammer_user_by_email('myemail@test.com');
$ymuser_id = $ymuser->id;
$user_token = yammer_oath_token($ymuser_id);
$group = yammer_group_by_name('group_name_goes_here');
yammer_group_join($group->id,$user_token->token);

Global variables in your inc file, these come from your app settings and then from authenticating the admin acct the first time. Follow these instructions to generate the token for your admin accout http://developer.yammer.com/authentication/#a-testtoken

$YAMMER_CLIENT_ID = "my_app_client_id";
$YAMMER_CLIENT_SECRET = "my_app_secret";
$YAMMER_DOMAIN = "https://test.com";
$YAMMER_ADMIN_TOKEN = "my_yammer_admin_token";

Here are the different functions that would go in you inc file

function yammer_user_by_email($email, $token = null){
    global $YAMMER_ADMIN_TOKEN;

    $user = yammer_api_get('https://www.yammer.com/api/v1/users/by_email.json?email='.$email, $YAMMER_ADMIN_TOKEN);

    return $user[0];

}

function yammer_oath_token($user_id, $token_index = 0){
    global $YAMMER_CLIENT_ID;
    global $YAMMER_ADMIN_TOKEN;

    $user_token = yammer_api_get('https://www.yammer.com/api/v1/oauth/tokens.json?consumer_key='.$YAMMER_CLIENT_ID.'&user_id='.$user_id, $YAMMER_ADMIN_TOKEN);

    if ($token_index == -1){
      return $user_token;
    }

    return $user_token[$token_index];
}

function yammer_group_by_name($name){
    global $YAMMER_ADMIN_TOKEN;

    $list = yammer_api_get("https://www.yammer.com/api/v1/search.json?search=$name", $YAMMER_ADMIN_TOKEN);

    if ($list->groups) foreach ($list->groups as $group) {
        if ($group->name == $name) return $group;
    }

    return null;

}

function yammer_group_join($group_id, $token = null) {
    yammer_api_post("https://www.yammer.com/api/v1/group_memberships.json?group_id=$group_id", "group_id=$group_id", $token);
}

This is the function that you will route all of the http calls through. The admin token goes in the header for all of the calls.

function yammer_api_call($url, $method = 'GET', $body = '', $token){

  if ($token == null) {
    if (!$_SESSION['yammer_token'] || !$_SESSION['yammer_token']->access_token->token) return false;
    $token = $_SESSION['yammer_token']->access_token->token;
  }

  if ($method == 'GET'){
    $opts = array('http' =>
      array(
        'method'  => $method,
        'header'  => "Host: www.yammer.com\r\n" 
            ."Authorization: Bearer " . $token . "\r\n"
      )
    );
  }else{
    $opts = array('http' =>
      array(
        'method'  => $method,
        'header'  => "Content-Type: application/x-www-form-urlencoded\r\n"
            ."Host: www.yammer.com\r\n" 
            ."Authorization: Bearer " . $token . "\r\n"
            ."Content-Length: " . strlen($body) . "\r\n",
        'content' => $body,
        'timeout' => 60
      )
    );
  }

  $context  = stream_context_create($opts);
  $resp = file_get_contents($url, false, $context);
  $resp_obj = json_decode($resp);
  return $resp_obj;

}

That should do it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top