How to send html gmail message through javascript gapi? - javascript

I am trying to send email message using JavaScript Gmail API, but I cannot find how to do it.
Well, I have code to send normal email message, but all html is displayed like text.
Here is code:
var email = "From: me\r\nTo: "+to+ "\r\nBcc: " +bcc+ "\r\nSubject: "+subject+"\r\n\r\nbody: " + content;
SEND MESSAGE WITH METADATA ONLY
function sendMessage(email,id)
{
var base64EncodedEmail = btoa(unescape(encodeURIComponent(email))).replace(/\//g,'_').replace(/\+/g,'-');
var request = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': base64EncodedEmail
},
});
request.execute(callbacksend);
}
When I have tried to set content type as html in response, I receieved an error response from server that I need to use upload method.
Indeed, here - https://developers.google.com/gmail/api/v1/reference/users/messages/send
Is said:
Upload URI, for media upload requests:
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send
but while trying to use
gapi.client.load('upload', 'v1', callback);
or just trying to run gapi.client.upload.gmail.users.messages.send
its not found...
I have no idea how to perform such request, or another way to send html message from gmail. Perhaps I need to try it from pure XMLHttprequest, but better to find the way to perform it from gapi.
p.s I've made such request through google apps script:
GmailApp.sendEmail(<recipient>,'subject', <message>,{htmlBody:<message>, bcc:'test#gmail.com'});
But need to perform it with javascript.
Thanks!

Related

Sending URL with Nodejs and Get Response

Hey guys can someone help me with that issue
im new on node.js and im studing and starting a project.
what i need to do is:
i need to send an url to a webservice and get the response from the webservice, for example:
i send http://www.webservice.com.br/getserver?user=myuser&password=mypassword
after that the webservice will give me a response at his page a succes/error message or a new url for login example http://history.webservice.com.br
so i need to get the response after sending an url someone know how can i do that with node.js ?
in php would be something like that
/*
* $sendurl = "myUrlHere";
* $getserver = file($sendurl);
* var_dump($getserver);
*/
in this php example i send the url and i get the response withou leaving my page.
if someone know how i do that in node or give me a way i will appreciate.
Thank you so much.
First of all, sending username passwords directly in url get requests is a disaster! By the way, I think you are just playing around with node, so this is forgivable ;) Following is a sample code for you to do http requests to your web service:
var http = require('http');
http.get("http://www.webservice.com.br/getserver?user=myuser&password=mypassword", function(res) {
//this is http response status code
console.log(res.statusCode);
//this is headers
console.log(res.headers);
res.setEncoding('utf8');
res.on("data", function(chunk) {
//this is body
console.log(chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

Struggling to communicate with the utorrent web API

I am trying to access utorrents web api, it uses a token authentication system which is detailed here
the JavaScript on my page is
<script>
$.getJSON("http://XXX.XXX.XXX.XXX/lib/token.php", function(response) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
//script.onreadystatechange = function () {
// if (this.readyState == 'complete') utorrent();
//}
//script.onload = utorrent();
script.src = 'http://XXX.XXX.XXX.XXX:8080/gui/?list=1&token=' + response.token;
head.appendChild(script);
});
</script>
simply retrieving the token from a php file and passing it along the chain, i have confirmed that the token is being passed and is not being poisonned, my PHP document is below
<?php
header('Content-type: text/json');
$token = file_get_contents('http://[username]:[password]#XXX.XXX.XXX.XXX:8080/gui/token.html');
$token = str_replace("<html><div id='token' style='display:none;'>", "", $token);
$token = str_replace("</div></html>", "", $token);
$response = array('token' => $token);
echo json_encode($response);
?>
this gives me a confirmation of the token
Object {token: "GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA"}
but then i receive a 400 error code when retrieving the list
GET http://XXX.XXX.XXX.XXX:8080/gui/?list=1&token=GMt3ryaJE64YpXGN75-RhSJg-4gOW8n8XfTGYk_ajpjNLNLisR3NSc8tn1EAAAAA 400 (ERROR)
Any help/thoughts/idea's would be greatly appreciated
just adding my 2 cents.
I've been doing a similar implementation in .NET MVC - I was able to get the token as you did, but the list=1 feature didn't work for me either, getting the 400 bad request code (as you have found).
The solution for me:
In the token.html response, there is a token in the div and also a GUID in the header.
To break it down:
Call token.html with uTorrent credentials
In the response content, parse the html to get the token
in the response header, there is a value with key Set-Cookie, which looks like
Set-Cookie: GUID=<guid value>
I needed to use this value (GUID=<guid value>) in all requests being sent back, as well as the token and it worked!
I'm not sure what the implementation is in PHP to do this however :)
Also quick note, I've been trying to get values through jQuery's $.getJSON and $.Ajax method without any success, because the browser (chrome) I'm using has strict guidelines on cross domain requests, and it doesn't look like uTorrent is implementing JSONP.
Hope this helps!
The 400 Error message means you are communicating with a bad request.
The MIME media type for JSON text is application/json .
use text/plain or application/json, not text/json.
application/json sometimes causes issues on Chrome, so you might want to stick with text/plain in this case.
Have you tried changing the order of the query parameters?
eg: http://localhost:8080/gui/?token=<token_uuid>&list=1
Reference: https://github.com/bittorrent/webui/wiki/TokenSystem#examples
UPDATE
I ran into a similar problem trying to create and XMPPBot for utorrent client in python.
#m.t.bennett was correct. You need to save the session information as well.
When you receive the response from token.html, capture the cookie information as well.
Usually there are 2 params: GUID and sessions. You need to put them in the header for all your subsequent requests -- List API, Getfiles API, etc.
This should fix your problem!

How do I catch the event and update my page when a user accepts a request?

I want to invite my friends to an application using the following codes . However, I want to catch the event when the invitee accepts the request. That is as soon as the user accepots the request, I update my page by marking him as "Accepted" . I have no idea how to catch that event ..Anyone can help me out
I have the following code ;
function sendRequest() {
FB.ui({method: 'apprequests',
message: 'You have received a pretty image from a friend.',
data: 'some tracking info',
title: 'Select friends to send your image to.'}, function (response) {
if (response && response.request_ids) {
$(function(){
//get friends who accepted the request
} else {
//Do something if they don't send it.
}
})
}
Once user accepts a request he is redirected to your application URL with request_ids GET parameter with contain ids of requests.
You can get info on request by issuing GET request to https://graph.facebook.com/USERID_REQUESTID
You shoul read documentation on Requests Dialog for more details.

How do I check if a user has accepted my application request in facebook?

I send my app request using the following method
function sendRequest() {
FB.ui({method: 'apprequests',
message: 'You have received a pretty image from a friend.',
data: 'some tracking info',
title: 'Select friends to send your image to.'}, function (response) {
if (response && response.request_ids) {
$(function(){
//get friends who accepted the request
} else {
//Do something if they don't send it.
}
})
}
However, I have no idea how to check if any friends have accepted the request ?
Any idea how to do that?
I also want to get a notification when a user accepts the request ...How do I achieve that?
You need to save the response.request_ids and then you can use open graph to query information on those requests. Get an access/auth token using app login, then query the graph api:
https://graph.facebook.com/[request_id]?access_token=[token]
But even without doing that, you should just be recording that the user responded when they click on the "invite". Facebook passes a request_ids parameter in the URL. You should check for that when a user comes to your app. Then you can update your information that the user responded to the request.

I dont understand how to implement the Deleting Requests in request dialog

I have read the document here
but I don't understand how should I implement it right.
In my facebook app I use apprequests from JS api like this:
function newInvite() {
var msg = document.getElementById('msg_look_id').value;
var receiverUserIds = FB.ui({
method: 'apprequests',
message: msg,
title: "Select friends to send your gift",
},
function (response) {
alert("IDS : " + response.request_ids);
});
//http://developers.facebook.com/docs/reference/dialogs/requests/
}
then the user see the request in its app request icon (with the red numbers )
the user click it , but then how do I implement the delete request ?
When the user arrives at your app’s canvas page following a request, there is a parameter called request_ids passed to your app.
Since Facebook calls apps in the iframe using the HTTP method POST, I guess this is also a POST parameter (although it is not explicitly mentioned in the docs). That means you have no access to it using pure client-side JavaScript; you have to use a server-side script to get the content of this parameter.

Categories

Resources