我目前正在尝试升级我的应用程序,以根据新的要求通过标题而不是通过查询字符串发送OAuth令牌。在标头中使用OAuth令牌发出GET请求时,我的请求会成功验证有效的access_token。但是,当尝试使用相同的令牌发布帖子时,我会收到未经授权的401。当访问令牌放在查询字符串上时,具有相同访问令牌的帖子会成功。
var request = (HttpWebRequest)WebRequest.Create(yammerurl);
request.Method = "POST";
request.Headers["Authorization"] = "Bearer " + access_token;
request.Host = "www.yammer.com";
request.ContentType = "application/json;charset=utf-8";
这是我的发布设置,正在接收未经授权的例外,下面是我为成功的GET请求设置的。同样,它们都使用相同的访问令牌,当访问令牌通过查询字符串传递时,这两种方法都有效。
string url = "https://www.yammer.com/api/v1/groups.json?mine=1";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers["Authorization"] = "Bearer " + YammerAccessToken;
request.Host = "www.yammer.com";
有人知道我的发布设置是否不正确,或者是否需要添加其他参数?
答案 0 :(得分:0)
是不是因为你没有在第二个中指定HTTP动词GET?这就是我在PHP中完成它的方法,它工作得很好......
我在我的主要php文件中调用它...
$ymuser = yammer_user_by_email('myemail@test.com');
此功能在我的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];
}
我的所有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);
//print($resp);
$resp_obj = json_decode($resp);
return $resp_obj;
}