response detail error:2,errMsg:post method api body check failed! Agora REST API - node.js

const Authorization = `Basic ${Buffer.from(`${config.CUSTOMERID}:${config.CUSTOMER_SECRET}`).toString("base64")}`;
const acquire = await axios.post(`https://api.agora.io/v1/apps/${config.agoraAppId}/cloud_recording/acquire`,{
cname: groupId,
uid: userId,
clientRequest: {
},
},
{ headers: { Authorization } }
);
Getting the following response when i call agora live streaming cloud recording from nodejs code using axios.
{
code: 2,
reason: 'response detail error:2,errMsg:post method api body check failed!'
}

This means you have been successfully Authorized but you are passing either wrong value or it is not defined in your body or URL.
Please check if any variable you are passing is undefined or contains wrong/incomplete value.

You should pass the header as an object with they Authorization key, in your case:
{
headers: {'Authorization': "Basic " + Authorization}
}

Related

Somebody knows how to solve 401 Unauthorized error with Clockify API using axios and node js

Does someone know why I'm getting a 401 Unauthorized error when I try to post a request to add a time entry even though I have added my API key?
I'm using Axios and node js, and this is the information and how I am making the request. However, I am able to get a list of users using the axios.get() method with the same API key, so I'm not sure why the post request says I'm not Unauthorized.
const insertOwnTimeEntryUrl = `${url}/workspaces/${workspaceId}/time-entries`;
let payload = {
start: "2022-05-05T17:10:00.000Z",
billable: "true",
description: "Test insert 1",
projectId: projectIdToInsert,
taskId: taskIdToInsert,
end: "",
tagIds: [tagIdToInsert],
};
let headers = {
"X-Api-Key": key,
};
let clockifyData = {
headers,
payload,
};
async function addNewEntry(clockifyData, insertOwnTimeEntryUrl, key) {
return (response = await axios
.post(`${insertOwnTimeEntryUrl}`, {
data: clockifyData.payload,
headers: {
"X-Api-Key": key,
"Content-Type": "application/json",
},
})
.catch(function (error) {
console.log(error.response.data.message);
return;
}));
}
addNewEntry(clockifyData, insertOwnTimeEntryUrl, key);

'Full authentication is required to access this resource' Clockify API with Node JS

I am unable to add new clients using the Clockify API in Node js. The following axios post request returns the error message: 'Full authentication is required to access this resource'. However, I am able to get a list of clients using the axios.get() method with the same API key, so I'm not sure why the post request says I'm not authenticated. Please let me know what I'm missing
async function addNewClient(clientName) {
return response = await axios.post(`${url}/workspaces/${workspaceId}/clients`, {
data: {
'name': clientName
},
headers: {
'X-Api-Key': CLOCKIFY_API_KEY,
'Content-Type': 'application/json',
},
}).catch(function (error) {
console.log(error.response.data.message);
return
});
}
I had the same issue. Regenerating API_KEY solved it.

Firebase Cloud Functions: Requests from referer <empty> are blocked. - PERMISSION_DENIED

I am not sure why the following code is throwing google api key permission denied.
I have the api or service enabled both in firebase console and google console.
export async function createJobDynamicLink(job){
if(job.jobStatus !== 'approved' || (job.dynamicLink).length > 2){
console.log('Dynamic link already exist!');
return false;
}
console.log(dynamic_links);
console.log(dynamic_links_key);
// Firebase web api key logs just fine
const options = {
method: 'POST',
uri: `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${dynamic_links_key}`,
body: {
"longDynamicLink": makeDynamicLongLink(job)
},
json: true
};
return await requestpromise(options)
.then(function (parsedBody) {
console.log(parsedBody);
return parsedBody.shortLink;
})
.then((shortLink) => {
//post.shareUrl = shortLink;
console.log('short link: ' + shortLink);
//return event.data.ref.set(post);
return shortLink;
})
}
export async function makeDynamicLongLink(job) {
return buildUrl(`${dynamic_links}`, {
queryParams: {
link: `https://app.com/jobs/${slugify(job.jobTitle)}-${job.id}`,
apn: "com.app.appe",
ibi: "com.app.app",
dfl: "https://app.com",
st: job.jobTitle,
}
});
}
Is something wrong with the way I am doing the request using request-promise?
StatusCodeError: 403 - {
"error": {
"code": 403,
"message": "Requests from referer <empty> are blocked.",
"status": "PERMISSION_DENIED",
"details": [{
"#type": "type.googleapis.com/google.rpc.Help",
"links": [{
"description":"Google developer console API key",
"url": "https://console.developers.google.com/project/904573jjwj/apiui/credential"
}]
}]
}
}
Go to the Google API Credentials https://console.developers.google.com/apis/credentials and see if there is any restriction on your API Key you're using.
If it is restricted by HTTP referrers, then add your website domain to it and add the Referrer header like the above answer.
Although in your use case, None or IP address restriction is a better choice.
Because you are invoking your function from a node.js environment, the HTTP Header Referer isn't being set. When you create requests through a browser, the browser will automatically fill this field for you.
You can get a suitable referrer value using:
"https://" + process.env.GCLOUD_PROJECT + ".cloudfunctions.net/createJobDynamicLink"
// becomes "https://your-project-id.cloudfunctions.net/createJobDynamicLink"
This generated URL isn't callable, because it doesn't start with a region, but it means you now have a URL that can be used to identify that the call is coming from a Cloud Function.
To use it, add it to your request-promise options object.
const options = {
method: 'POST',
uri: `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${dynamic_links_key}`,
body: {
"longDynamicLink": makeDynamicLongLink(job)
},
headers: {
"Referer": "https://" + process.env.GCLOUD_PROJECT + ".cloudfunctions.net/createJobDynamicLink"
},
json: true
};
Note: I'd use request-promise-native instead of request-promise - same API but skips loading Bluebird.
In my case i had
<meta name="referrer" content="no-referrer">
in the head so the referer was not being sent

401 unauthorized Authentication parameters missing in dialogflow api

I have developed chatbots using Dialogflow api v1. There had been no change in the code. But, now the response from the server is "code":401,"errorType":"unauthorized","errorDetails":"Authentication parameters missing. I checked in Postman using the below url.
https://api.dialogflow.com/v1/query?v=519794&lang=en&query=hi&sessionId=519794&Content-Type=application/json and gave my client access token in bearer token. When I tried with post method I got error.But, when i used GET method it worked in postman. So, I tried the same code in my javascript code and got 401 unauthorized error. What's wrong here?
I have resolved this issue using the following code.
Used GET method instead of POST in Ajax call and removed JSON.stringify in the data parameter.
var text = "hello";
var iRandom = Math.floor((Math.random() * 10000000) + 1);
$.ajax({
type: "GET",
url: "https://api.api.ai/v1/query?v=324233",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer "+accessToken
},
data: { query: text, lang: "en", sessionId: iRandom},
success: function(data) {
//desired action
},
error: function() {
console.log("Internal Server Error");
}
});
Another alternate solution:
I retained the earlier version.
var iRandom = Math.floor((Math.random() * 10000000) + 1);
type: POST,
data: JSON.stringify({ query: text, lang: "en", sessionId: iRandom}),
Instead of passing the iRandom as number, I passed it as String and I was able to get response from the dialogflow.

Spotify API Authorization Grant Flow status 415

I am trying to build a web app that integrates with the spotify API. For this, I am using the Authorization grant flow.
I managed to get an authorization code, but on the back end when I am testing the endpoint that should exchange the auth code with an access token, I keep getting a 415 response status.
Here is the service that the endpoint is using:
export async function getAccessAndRefresh(code: string): Promise<any> {
return axios.post(ACCESS_URL, {
data: {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": REDIRECT_URI
},
headers: {
"Authorization": " Basic " + Buffer.from(CLIENT_ID + ":" + CLIENT_SECRET).toString("base64"),
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
json:true
})
}
Also, I wrote this unit test in order to test the service(I got the 415 while running this unit test):
describe("Request tests", () => {
let server: Server;
function initServer() {
server = createServer(App);
server.listen(5000);
}
function destroyServer() {
server.close();
}
test("Test refresh and access token returned by spotify api", () => {
return getAccessAndRefresh(AUTH_CODE).then((value)=>{
expect(value).toHaveProperty("access_token");
})
})
beforeAll(() => {
initServer();
});
afterAll(()=>{
destroyServer();
})
})
In the test, AUTH_CODE is a code that I obtained manually in a browser by accessing the https://accounts.spotify.com/authorize endpoint with my API Key.
Can anyone help me figure this one out please? Thanks!
In the Spotify Dashboard you might need to set the Redirect URI to the URL you're using in your code, these need to match if getting the following error:
{ "error": "invalid_grant", "error_description": "Invalid redirect URI" }
That's all you need to do, just go to the Dashboard where you get the Client ID and Client Secret and then go to the Edit Settings and you'll see the option to set the Redirect URI

Resources