您可以使用管理用户的 Oauth 令牌来查找另一个用户令牌。然后使用该用户令牌,通过指定组名将他们加入组。以下是我们在 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);
您的 inc 文件中的全局变量,这些变量来自您的应用程序设置,然后来自第一次验证管理员帐户。按照这些说明为您的管理员帐户生成令牌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";
以下是您的 inc 文件中的不同功能
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);
}
这是您将路由所有 http 调用的函数。管理令牌位于所有调用的标头中。
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;
}
那应该这样做。