How to send Outlook messages by javascript SDK? - microsoft-graph-api

I searched for a long time but I did not find a relevant solution.
When I send mail via Microsoft Graph API to get 403, I do not know where the problem is
{
statusCode: 403,
code: "ErrorAccessDenied",
message: "Access is denied. Check credentials and try again."
}
Code:
makeSilentTokenRequest
//The static key authentication request
function makeSilentTokenRequest(callback) {
// Build up a hidden iframe
var iframe = $('<iframe/>');
iframe.attr('id', 'auth-iframe');
iframe.attr('name', 'auth-iframe');
iframe.appendTo('body');
iframe.hide();
iframe.load(function () {
callback(localStorage.accessToken);
});
iframe.attr('src', buildAuthUrl() + '&prompt=none&domain_hint=' +
localStorage.userDomainType + '&login_hint=' +
localStorage.userSigninName);
}
getAccessToken
//Verify refresh token function
function getAccessToken(callback) {
var now = new Date().getTime();
var isExpired = now > parseInt(localStorage.tokenExpires);
// Do we have a token already?
if (localStorage.accessToken && !isExpired) {
// Just return what we have
if (callback) {
callback(localStorage.accessToken);
}
} else {
// Attempt to do a hidden iframe request
makeSilentTokenRequest(callback);
}
}
getUserEmailAddress
function getUserEmailAddress(callback) {
//I saved it in localStorage
if (localStorage.userEmail) {
return localStorage.userEmail;
} else {
//getAccessToken
getAccessToken(function (accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
// Get the Graph /Me endpoint to get user email address
client
.api('/me')
.get((err, res) => {
if (err) {
callback(null, err);
} else {
callback(res.mail);
}
});
} else {
var error = {
responseText: 'Could not retrieve access token'
};
callback(null, error);
}
});
}
}
sendMail
//sendMail
function sendMail(message) {
getUserEmailAddress(function (userEmail, error) {
if (error) {
renderError('getUserEmailAddress failed', error.responseText);
} else {
getAccessToken(function (accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
client
.api('/me/sendMail')
.header('Authorization', 'Bearer ' + accessToken)
.header('Content-Type', "application/json")
.post(message, (err, res) => {
if (err) {
console.log(err);
//callback(null, err);
} else {
console.log(res);
//callback(res.value);
}
});
} else {
var error = {
responseText: 'Could not retrieve access token'
};
//callback(null, error);
}
});
}
})
}
createMail
function createMail(message,callback){
getUserEmailAddress(function(userEmail, error) {
if (error) {
renderError('getUserEmailAddress failed', error.responseText);
} else {
getAccessToken(function(accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
client
.api('/me/messages')
.header('Authorization', 'Bearer ' +accessToken)
.header('Content-Type', "application/json")
.post(message,(err, res) => {
if (err) {
callback(res);
console.log("创建失败!");
} else {
callback(res);
console.log("创建成功!");
}
});
} else {
var error = { responseText: 'Could not retrieve access token' };
//callback(null, error);
}
});
}
})
}

There isn't enough to go on here but the error message is telling you that your accessToken isn't valid.
I can't see which scopes are you requesting or which authorization workflow you executing so I can't pinpoint precisely why it is invalid.

Related

From the two same lines, one is generating an error relative to FutureOrNull

Thos are the two function from the same class I wrote: The first has the error:
A value of type 'Future<String?>' can't be returned by the 'onError' handler because it must be assignable to 'FutureOr'.
while the second is OK and I don't understand why.
Can you tell me please?
/// Creates or update the User collection
Future<String?> _createOrUpdateUser(User user) {
final email = user.email;
final nameFromEmail =
email!.substring(0, email.indexOf('#')).replaceAll(".", " ").trim();
return FirebaseFirestore.instance.collection('users').doc(user.uid).set({
'id': user.uid,
'screenName': '',
'displayName': user.displayName ?? nameFromEmail,
'photoUrl': user.photoURL,
'bio': '',
'darkMode': false,
'email': user.email,
}).then((_) {
debugPrint("User ${user.displayName} is created");
return null;
}).catchError((e) {
debugPrint(e.toString());
return Future<String?>.error(e); // <<<<< Error here: A value of type 'Future<String?>' can't be returned by the 'onError' handler because it must be assignable to 'FutureOr<Null>'.
});
}
Future<String?> _authUser(BuildContext context, LoginData data) {
debugPrint('Name: ${data.name}, Password: ${data.password}');
return auth
.signInWithEmailAndPassword(email: data.name, password: data.password)
.then((credential) {
try {
final user = credential.user;
if (user == null) {
return 'Password does not match';
} else {
_createOrUpdateUser(user);
// == Tell the app we just signed in
Provider.of<StateModel>(context, listen: false).signedIn = user;
return null;
}
} catch (e) {
return 'User not exists';
}
}).catchError((e) {
if (e.code == 'weak-password') {
debugPrint('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
debugPrint('The account already exists for that email.');
} else {
debugPrint(e.toString());
}
return Future<String?>.error(e); // <<<<< No problem here
});
}

Netlify functions with axios

I want to make my form submission happen server-side in order to not expose my API key. I plan to do this with netlify functions however I don't know how that would look with Axios. I've looked for examples on how to do this but I don't seem to find any. Could some help me I'm stuck as to what to put inside my the Netlify function? If anyone has worked with these two programs and could provide a hand that would be helpful here is my javascript with my submission function.
var form = document.querySelector("#user_form");
let reqHeaders = {
headers: {
Authorization: "Bearer",
}
}
let url = ""
let reqData = {
records: [
{
fields: null
}
]
}
let formData = {
firstName: "",
lastName: "",
email: ""
}
function logData(id, dataObj, value) {
dataObj[id] = value;
console.log(value)
}
function formMessg (id) {
document.querySelector(id).style.display = "block";
setTimeout(function(){
document.querySelector(id).style.display = "none";
form.reset();
}, 2500)
}
form.addEventListener("submit", function (e) {
e.preventDefault();
let spam = document.getElementById('spam').value;
try {
for(const data in formData){
if(formData[data] === "" || spam.length !== 0){
const error = new Error();
error.notVaild = true;
throw error;
}
}
reqData.records[0].fields = formData;
console.log(reqData);
axios.post(url, reqData, reqHeaders).then((res) => {
formMessg ('.success-messg');
form.style.display = "none";
})
.catch ((err) => {
throw err;
});
} catch (err){
if (err.reponse){
formMessg ('.fail-messg');
} else if (err.request) {
formMessg ('.fail-messg');
} else if ("Notvalid") {
formMessg ('.fill-messg');
}else {
console.log(err);
}
}
});

How to handle user cancel for OAUTH2 request in Teams messaging extension

The cancellation response from token.botframework.com is currently being displayed to screen like this:
{
"error": {
"code": "ServiceError",
"message": "Missing required query string parameter: code. Url = https://token.botframework.com/.auth/web/redirect?state=d48fb60ae4834fd8adabfe054a5eff74&error_description=The+user+chose+not+to+give+your+app+access+to+their+Dropbox+account.&error=access_denied"
}
}
How can I, instead, handle the cancellation gracefully? If the user cancels like this, I'd like to just have the auth-card-popup window close automatically.
This is for an action-type messaging extension app that I'm building. The sign-in process begins with an auth card. The bot is pointed at a Dropbox OAUTH2 connection. Here the relevant code that brings up the card:
const { TeamsActivityHandler, CardFactory } = require('botbuilder');
class MsgExtActionBot extends TeamsActivityHandler {
constructor() {
super();
this.connectionName = 'oauth2-provider';
}
async handleTeamsMessagingExtensionFetchTask(context, action) {
if (!await this.isAuthenticated(context)) {
return this.getSignInResponse(context);
}
}
async isAuthenticated(context) {
let tokenResponse = await context.adapter.getUserToken(
context,
this.connectionName
);
if (tokenResponse && tokenResponse.token) {
return true;
}
if (!context.activity.value.state) {
return false;
}
tokenResponse = await context.adapter.getUserToken(
context,
this.connectionName,
context.activity.value.state
);
if (tokenResponse && tokenResponse.token) {
return true;
}
return false;
}
async getSignInResponse(context) {
const signInLink = await context.adapter.getSignInLink(context, this.connectionName);
return {
composeExtension: {
type: 'auth',
suggestedActions: {
actions: [{
type: 'openUrl',
value: signInLink,
title: 'Please sign in'
}]
},
}
};
}
}

How to resolve `Uncaught (in promise) DOMException: Quota exceeded` error in service worker when browsing website in `Incognito mode` in Google chrome

How to resolve(or hide) Uncaught (in promise) DOMException: Quota exceeded error in service worker when browsing website in Incognito mode in Google chrome.
Every thing works fine in normal mode.
Service worker code of my progressive web app is
var version = 'v2:';
var offlineFundamentals = [
'/',
'/offline.html'
];
var updateStaticCache = function () {
return caches.open(version + 'fundamentals').then(function (cache) {
return Promise.all(offlineFundamentals.map(function (value) {
var request = new Request(value);
var url = new URL(request.url);
if (url.origin != location.origin) {
request = new Request(value, {
mode: 'no-cors'
});
}
return fetch(request).then(function (response) {
var cachedCopy = response.clone();
return cache.put(request, cachedCopy);
});
}))
})
};
var clearOldCaches = function () {
return caches.keys().then(function (keys) {
return Promise.all(keys.filter(function (key) {
return key.indexOf(version) != 0;
}).map(function (key) {
return caches.delete(key);
}));
});
};
var limitCache = function (cache, maxItems) {
cache.keys().then(function (items) {
if (items.length > maxItems) {
cache.delete(items[0]);
}
})
};
var trimCache = function (cacheName, maxItems) {
caches.open(cacheName).then(function (cache) {
cache.keys().then(function (keys) {
if (keys.length > maxItems) {
cache.delete(keys[0]).then(trimCache(cacheName, maxItems));
}
});
});
};
var hasUrlCacheExcludeMatch = function (url) {
var cacheExcludeUrls = [
"https:\/\/example.com\/user\/login",
"https:\/\/example.com\/user\/register"
];
return cacheExcludeUrls.some(path => url.includes(path));
};
self.addEventListener("install", function (event) {
event.waitUntil(updateStaticCache().then(function () {
return self.skipWaiting();
}));
});
self.addEventListener("message", function (event) {
var data = event.data;
if (data.command == "trimCache") {
trimCache(version + "pages", 80);
trimCache(version + "images", 50);
trimCache(version + "assets", 50);
}
});
self.addEventListener("fetch", function (event) {
var fetchFromNetwork = function (response) {
var cacheCopy = response.clone();
if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
if (!hasUrlCacheExcludeMatch(event.request.url)) {
caches.open(version + 'pages').then(function (cache) {
cache.put(event.request, cacheCopy).then(function () {
limitCache(cache, 80);
})
});
}
} else if (event.request.headers.get('Accept').indexOf('image') != -1) {
caches.open(version + 'images').then(function (cache) {
cache.put(event.request, cacheCopy).then(function () {
limitCache(cache, 50);
});
});
} else {
caches.open(version + 'assets').then(function add(cache) {
cache.put(event.request, cacheCopy).then(function () {
limitCache(cache, 50);
});
});
}
return response;
}
var fallback = function () {
if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
return caches.match(event.request).then(function (response) {
return response || caches.match('/offline.html');
})
}
}
if (event.request.method != 'GET') {
return;
}
if (event.request.headers.get('Accept').indexOf('text/html') != -1) {
event.respondWith(fetch(event.request).then(fetchFromNetwork, fallback));
return;
}
event.respondWith(caches.match(event.request).then(function (cached) {
return cached || fetch(event.request).then(fetchFromNetwork, fallback);
}))
});
self.addEventListener("activate", function (event) {
event.waitUntil(clearOldCaches().then(function () {
return self.clients.claim();
}));
});
Browsing website in Normal mode in Google chrome works fine no error occures in console.
I am not good in service worker so I am unable to resolve this issue.
This is a bit unobvious.
Quota exceed error means that your out of available space, which is 6% of total space for Chrome.
For incognito mode all storage and workers api is working, but quota space is equals zero bytes and, thus, you can not write to it.

Parse: Failed with: success/error was not called

I am trying to authenticate my social media accounts through Parse via OAuth and it authenticates fine but i keep getting a Failed with success/error was not called; my code is below, can anyone help?
Parse.Cloud.define("createNewNetwork", function(request, response) {
Parse.Cloud.useMasterKey();
//console.log(request.user.id);
userHasRole(request.user.id, 'AllUsers').then(function(hasRole){
if (hasRole){
var Network = Parse.Object.extend("Network");
var query = new Parse.Query(Network);
query.equalTo("userId", request.params.userId);
query.equalTo("owner", request.user);
query.first({useMasterKey:true}).then(function(network) {
var newNetwork;
if (!network) {
newNetwork = new Network();
var custom_acl = new Parse.ACL();
custom_acl.setWriteAccess(request.user, true);
custom_acl.setPublicReadAccess(true);
newNetwork.setACL(custom_acl);
newNetwork.set("userId", request.params.userId);
newNetwork.set("followingCount", request.params.followingCount);
newNetwork.set("owner", request.user);
newNetwork.set("userData", request.params.userData);
newNetwork.set("networkName", request.params.networkName);
newNetwork.set("screenName", request.params.screenName);
} else {
newNetwork = network;
}
newNetwork.set("tokenExpired", false);
//console.log(request.params.oAuthData["access_token"]);
if (request.params.networkName == "facebook-page") {
Parse.Cloud.httpRequest({
url: 'https://graph.facebook.com/v2.1/oauth/access_token?grant_type=fb_exchange_token&client_id=**************&client_secret=***************&fb_exchange_token='+request.params.oAuthData["access_token"],
success: function(httpResponse2) {
//console.log(httpResponse2);
if (httpResponse2.status == 200) {
var data = {};
data["access_token"] = httpResponse2.text.substring(13);
newNetwork.save({oAuthData : data}, {
success: function(savedNetwork) {
response.success(savedNetwork);
},
error: function(error) {
response.error(error);
}
});
} else {
response.error("invalid token");
}
},
error: function(httpResponse2) {
console.log(httpResponse2);
response.error('Request failed with response code ' + httpResponse2.status);
}
});
} else {
newNetwork.save({oAuthData : request.params.oAuthData}, {
success: function(savedNetwork) {
response.success(savedNetwork);
},
error: function(error) {
response.error(error);
}
});
}
});
} else {
response.error("not in role");
}
});
});
Parse.Cloud.define("fetchNetworks", function(request, response) {
var attributesToHide = ["oAuthData"];
Parse.Cloud.useMasterKey();
userHasRole(request.user.id, 'AllUsers').then(function(hasRole){
if (hasRole){
var Network = Parse.Object.extend("Network");
var query = new Parse.Query(Network);
query.descending("followingCount");
var user = new Parse.User();
user.id = request.params.userToFetch;
user.fetch({}).then(function(user) {
query.equalTo("owner", user);
query.find().then(function(networks) {
networks.forEach(function(network) {
attributesToHide.forEach(function(attr) {
delete network.attributes[attr];
});
});
return response.success(networks);
});
});
} else{
response.success({super: false});
}
});
});
Line No - 101 - Removal of return should work.

Resources