我想在没有用户签名的情况下在yammer组中创建用户并通过模拟来实现。
代表其他用户获取access_tokes [获取未经授权的标题]: 我正在使用Fiddler发起GET请求以代表具有以下参数的其他用户获取访问令牌
https://www.yammer.com/api/v1/oauth/tokens.json - 使用了网址
参数user_id和consumer_key使用...
我总是收到未经授权的例外。任何人都可以在这方面帮助我。如何通过我的应用程序模拟并在yammer中创建用户。
答案 0 :(得分:1)
您可以使用管理用户的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;
}
应该这样做。