Sharepoint hosted app giving 403 Forbidden Error - sharepoint

I am developing sharepoint hosted app and i am using rest api by angular.js or jquery.So I dont accept the app list give an error 403 Forbidden
Rest call looks like
$http({
method: "GET",
url: appweburl + "/_api/SP.AppContextSite(#target)/web/lists/getbytitle('SurveyManager')/items?#target='" + hostweburl + "'",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": angular.element(document.querySelector('#__REQUESTDIGEST')).val()
}
});
Response is
{
"error": {
"code": "-2147024891, System.UnauthorizedAccessException",
"message": {
"lang": "en-US",
"value": "Access denied. You do not have permission to perform this action or access this resource."
}
}
}

You need to pass an access token to authenticate the request.

Related

Spotify Web API Error 400: "Only valid bearer authentication supported"

I'm trying to use the Spotify Web API to search for songs through node, but it keeps sending me a status 400 error with the message: "Only valid bearer authentication supported". This is my code:
app.get("/", (req, res) => {
let searchurl = "https://api.spotify.com/v1/search?";
request.post(
{
url: searchurl,
data: {
q: "john",
type: "album",
},
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " +
Buffer.from(client_id + ":" + client_secret).toString("base64"),
},
method: "POST",
},
function (e, r, body) {
console.log(body);
}
);
});
I don't understand what the issue is and have read through everything I could find, but got nowhere. Am I supposed to use a different access key?
Have a look at the Authorisation Guide for Spotify, you can use the Client Id and Client Secret to get an Access Token and it is that you send as part of an Authorization header when making the request to the Search for an Item endpoint you're trying to use

I need help trying to call 3rd party api with authentication from node server

I am trying to call 2 3rd party apis. 1st to login and receive Bearer token and 2nd to post message. 1st works fine and I get the token. but when I try to call second api to post message it fails, probably because I dont know how to set the received token in 2nd api
here is my code
var myJSONObject = {
"email": auth[0],
"password": auth[1]
};
req.post({
url: "{{server_url}}/auth/login",
method: "POST",
json: true,
body: myJSONObject
}, function (error, res, body){
if(error){
console.log(error.message);
} else {
var myJSONObject1 = {
"category":"SYSTEM",
"type": "ALERT",
"keywords":"FUNCTION|createSomethingLvl1",
"status":"UNREAD",
"from": "tenantadmin#tenantadmin.com",
"to": "someemail#gmail.com",
"subject": "Some nice subject",
"body": "Some detailed body that contains information that informs the person"
};
req.post({
url: "{{server_url}}/api/message",
method: "POST",
headers: {
"Authorization": res.body.access_token,
"Content-Type": "application/json"
},
json: true,
body: myJSONObject1
}, function (err, res1, body){
if(error){
console.log(err.message);
} else {
console.log(res1.body);
}
});
}
});
If this is a "typical" http bearer token, then you need the word "Bearer" in front of the token like this:
"Authorization": "Bearer " + res.body.access_token,
You can see examples of a Bearer token in the OAuth RFC 6750 where it shows the grammar as:
b64token = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
"Bearer" 1*SP b64token
And, here's an example from a Google API doc:
Authorization: Bearer AbCdEf123456
And, another example from an OAuth doc:
Authorization: Bearer vF9dft4qmT

Outlook api webhook validation error with node js

I'm facing a problem with the validation of the notification url when I want to register a subscription with the outlook API.
The options passed in the request are :
var optionsSubscription = {
url: "https://outlook.office.com/api/v2.0/me/subscriptions",
method: "POST",
headers: {
"authorization": "Bearer " + user.outlookCalAccessToken,
"accept": "application/json",
"ContentType": "application/json",
},
json: {
"#odata.type": "#Microsoft.OutlookServices.PushSubscription",
"Resource": "me/events",
"NotificationURL": "https://xxx/callback",
"ChangeType": "Created,Deleted,Updated"
},
"Content-Type": "application/json"
}
The response is the following :
Notification URL 'https://xxx/callback?validationtoken=N2FhY2JhNmItYTc2MC00MGUwLThmOGItZWQ2N2Q5Nzg5Y2Y2' verification failed System.Net.WebException:
The request was aborted: Could not create SSL/TLS secure channel.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.Exchange.OData.Model.Notifications.PushNotification.PushSubscriptionCallbackUrlValidationHelper.SendRequestAndVerifyResponse (Uri callbackUrl, PushSubscription pushSubscription).
When I request the notification url with Postman, it works and returns the validation token with a 200 status as expected.
The SSL certificate is generated with let's encrypt.

Google Maps Tracks API 401 Invalid Credentials

I do have checked validity of my access_token at https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=mytoken
which is replying as follows,
{
"issued_to": "myservice_account_emailid",
"audience": "myservice_account_emailid",
"scope": "https://www.googleapis.com/auth/tracks",
"expires_in": 1300,
"access_type": "offline"
}
which means still my token is valid, but if i make request to google tracks api from my application, using the same access_token, am getting response as below ,
Response: {
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Even i created new project in google developer console, and created new service account , and used the new credentials in application, but still getting same response. what could be the reason?
Found my self.
My error prone code is as below ,
var jwt = new googleapis.auth.JWT(client_email, keyFile, null, scopes, null);
jwt.authorize(function(jwtErr, tokens){
if(jwtErr){
return;
}
else{
headers = {
'content-type' : 'application/json;charset=utf-8',
'content-length': Buffer.byteLength(data),
'Authorization' : tokens.access_token
};
options = {
host: 'www.googleapis.com',
path: '/tracks/v1/entities/create',
headers : headers,
method : 'POST',
};
var post_req = https.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.on('error', function(error_msg){
console.log(error_msg);
});
post_req.write(data);
post_req.end();
}
});
The problem is in header object, 'Authorization' field is set to only access.token value, along with that we must specify token_type value also.
so header must be like below.
headers = {
'content-type' : 'application/json;charset=utf-8',
'content-length': Buffer.byteLength(data),
'Authorization' : tokens.token_type+' '+tokens.access_token
};
and now it solves 401 Invalid Credentials error.

SharePoint Hosted App Error "The given key was not present in the dictionary"

I am developing a SharePoint Hosted App on office 365 using REST API's and Anjular js. I am able to read the host list and when I try and update it give me this Error "the given key was not present in the dictionary". Can't figure out what needs to be done.
This is the code that actually performance the operation
$http(
{
method: "POST",
url: empMstDetails[0].__metadata.uri, // getSiteUrl + "/_api/web/lists/GetByTitle(‘Employee Master')/items(" + empMstDetails[0].Employee_x0020_ID + ")",
body: {
'__metadata': { 'type': 'SP.Data.Employee_x0020_MasterListItem' }, 'Title': 'TestUpdated'
},
headers: {
"Accept": "application/json;odata=verbose",
"IF-MATCH": "*",
"X-HTTP-Method":"MERGE",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length":0
}
}
You should use 'data' instead of 'body'. That should clear out this error message, which is kind of confusing. -sean

Resources