I'm trying to make a request to the Bitly API using the code below but I am receiving a 422 error message. I'm not using OAuth as it seems this is not required for single account users such as myself.
The documentation isn't too clear on how to make a request in this scenario.
function bitly() {
var long_url = "https://stackoverflow.com/questions/ask";
var apiv4 = 'https://api-ssl.bitly.com/v4/shorten';
var genericAccessToken = "xxxxx";
var params = {
method: "post",
headers: {"Authorization" : "Bearer " + genericAccessToken,
"Content-Type": "application/json"},
payload: {"group_guid": "string",
"domain": "bit.ly",
"long_url": long_url},
};
var res = UrlFetchApp.fetch(apiv4, params);
var obj = JSON.parse(res.getContentText());
}
Bitly error:
HTTP/1.1 422 Unprocessable Entity
appears when payload/request content is not a JSON string.
For example in PHP do:
$headers = [
'Authorization: Bearer '.$param_bitly_token,
'Content-Type: application/json',
];
$context = stream_context_create([
'http' => [
'header' => $headers,
'method' => 'POST',
'content'=> json_encode([
'long_url' => $param_long_url,
'title' => $param_title,
]),
],
]);
$response = file_get_contents('https://api-ssl.bitly.com/v4/bitlinks', 0, $context);
Important bit here is json_encode , as for most other API-s it would be http_build_query
You have to send content as JSON string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.