Adding a post to google blogger with R httr - r

I want to post a message to the Google blogger API with R, but I am puzzled how to format the request with R, httr and jsonlite. GET works, but the formatting of the POST message and the JSON isn't clear to me.
The example of a POST is shown at the Google developer page:
POST https://www.googleapis.com/blogger/v3/blogs/8070105920543249955/posts/
Authorization: /* OAuth 2.0 token here */
Content-Type: application/json
{
"kind": "blogger#post",
"blog": {
"id": "8070105920543249955"
},
"title": "A new post",
"content": "With <b>exciting</b> content..."
}
Creating the message:
amesg <- list(
kind = "blogger#post",
blog =list(id = "..."),
Title = "A new post",
content = "With exciting content...")
jsnmesge <- jsonlite::toJSON(amesg,pretty = TRUE, auto_unbox = TRUE)
httr::POST("https://www.googleapis.com/blogger/v3/blogs/.../posts/key=...",
body = jsnmesge)
Gives as response:
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;p...
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>

Related

Azure media player unable to play DRM content as a MS teams app

I'm trying to create a small app where I'm going to play a DRM encoded video using azure media player. It is showing error code "(0x10500001)"
[enter image description here][1]
FYI- Player is playing DRM video on all browsers perfectly.
Here is the URL for JSFiddle for AMP DRM Play - https://jsfiddle.net/f0pewmqz/
<!DOCTYPE html>
<html lang="en-US"><head><meta charset="utf-8">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Azure Media Player</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//amp.azure.net/libs/amp/latest/skins/amp-default/azuremediaplayer.min.css" rel="stylesheet">
<script src="//amp.azure.net/libs/amp/latest/azuremediaplayer.min.js"></script>
</head>
<body>
<h1>Sample: DRM - PlayReady, Widevine, and FairPlay (no token)</h1>
<video id="azuremediaplayer" class="azuremediaplayer amp-default-skin amp-big-play-centered" tabindex="0"> </video>
<script>
var myOptions = {
autoplay: true,
controls: true,
width: "640",
height: "400",
poster: ""
};
var myPlayer = amp("azuremediaplayer", myOptions);
myPlayer.src([{ src: "//ampvideos.streaming.mediaservices.windows.net/9b77ed82-94af-41bc-b08b-537e7a530eb6/sintel_trailer-1080p_fp.ism/manifest", type: "application/vnd.ms-sstr+xml", protectionInfo: [{ type: "PlayReady" }, { type: "Widevine" },{ type: "FairPlay", certificateUrl: "//yourdomain/fairplay.der" }] }, ]);
</script>
</body>
To quickly replicate the issue on MS Teams Desktop app and Browser app:
Copy the URL - https://dhananjaytest.w3spaces.com/saved-from-Tryit-2023-02-14-jhqwb.html
In MS Teams select add Tab option (+).
Select Website option.
Add URL and save.
If you are using Teams on browser you can replicate this by following link and follow the above steps.
https://dhananjaytest.w3spaces.com/saved-from-Tryit-2023-02-14-jhqwb.html

How to embed image inline(not attachment) in sendmailR email?

I have a code to send an image inline in body of email using sendmailR. However after receiving the email it does not contain the image. It shows the error : The linked image cannot be displayed.The file may have been moved,renamed or deleted.
Below is the code
library(sendmailR)
image<-"image.png"
body<-sendmailR::mime_part("<html><p>This is a picture.</p>
<img src='image.png' >
<p> Image is:</p>")
body[["headers"]][["Content-Type"]] <- "text/html"
sender <- "sender email"
recipients <- "receiver email"
subject <- "Test Email"
sendmailR:: sendmail(sender, recipients, subject, list(body),control=list(smtpServer="server address"))
The image is in the same folder as the rmd file.
Thank you.
I had the same question and I finally got it. Using your code should be something like:
library(sendmailR)
attachmentPath <-paste(getwd(),"/image.png",sep="")
attachmentObject <-mime_part(x=attachmentPath,name="plot.png")
msg <- mime_part('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
</style>
</head>
<body>
<h1>HTML email</h1>
<img src="cid:plot.png" >
</body>
</html>')
msg[["headers"]][["Content-Type"]] <- "text/html"
body <- list(msg,attachmentObject)
sender <- "sender email"
recipients <- "receiver email"
subject <- "Test Email"
sendmailR:: sendmail(sender, recipients, subject, body,control=list(smtpServer="server address"))
as you can see you must reference the image using cid

Only first two code snippets in my snippets.cson work in Atom

I want to use my own code snippets in Atom, but after hours of trying I am still not able to use more than first two of them and I cannot find a mistake.
When I was looking for a solution I found many, many topics but the mistake in each of them was repetitive usage of source scope but it is not my problem. I actually tried almost everything, I have made many changes in my syntax, but nothing helped.
".text.html.basic, .text.html.php":
"Include jQuery":
"prefix": "jquery"
"body": "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>"
"Basic HTML structure":
"prefix": "start"
"body": '''
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="utf-8">
<meta name="description" content="$1">
<meta name="author" content="MagicDev">
<meta name="keywords" content="$2">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
<title>$3</title>
</head>
<body>
$4
</body>
</html>
'''
"DB Connection":
"prefix": "connection"
"body": '''
<?php
$servername = "$1";
$username = "$2";
$password = "$3";
$dbname = "$4";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_query($conn, "set names utf8");
?>$5
'''
"DB Close Connection":
"prefix": "disconnection"
"body": "$conn->close();$1"
"DB Insert":
"prefix": "insert"
"body": '''
$sql = $conn->prepare("INSERT INTO ${1:"table"} (${2:"attributes"})
VALUES (${3:"?,?,?"})");
$sql->bind_param("${4:"ssi"}", ${5:"variablesToInsert"});
$sql->execute();
$sql->close();$6
'''
"Remove tags":
"prefix": "removeTags"
"body": "strip_tags($1)"
Can somebody save me, please? What am I overlooking?
Like CoffeeScript, CSON (CoffeeScript Object Notation) is indentation-sensitive. The first two snippets in your example are indented correctly, the following ones are one level too deep.
If you're unfamiliar with indentation-based language (and formats), I recommend using linter-coffeelint. It marks all kinds of syntactical errors.
Example:
If you feel more at home using JSON, Atom also supports snippets saved in that format.
Lastly, here are your snippets with correct indentation:
".text.html.basic, .text.html.php":
"Include jQuery":
"prefix": "jquery"
"body": "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>"
"Basic HTML structure":
"prefix": "start"
"body": '''
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="utf-8">
<meta name="description" content="$1">
<meta name="author" content="MagicDev">
<meta name="keywords" content="$2">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
<title>$3</title>
</head>
<body>
$4
</body>
</html>
'''
"DB Connection":
"prefix": "connection"
"body": '''
<?php
$servername = "$1";
$username = "$2";
$password = "$3";
$dbname = "$4";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_query($conn, "set names utf8");
?>$5
'''
"DB Close Connection":
"prefix": "disconnection"
"body": "$conn->close();$1"
"DB Insert":
"prefix": "insert"
"body": '''
$sql = $conn->prepare("INSERT INTO ${1:"table"} (${2:"attributes"})
VALUES (${3:"?,?,?"})");
$sql->bind_param("${4:"ssi"}", ${5:"variablesToInsert"});
$sql->execute();
$sql->close();$6
'''
"Remove tags":
"prefix": "removeTags"
"body": "strip_tags($1)"

Accessing FB Business Manager API

I am trying to access FB Business Manager Marketing API to get data on my Ad Set data.
The way I went about accessing this is by first creating an app through here: https://developers.facebook.com/apps
Should I use something else like Rfacebook? This is how I did it below:
I retrieved my App Id and App Secret.
From here, I made a call to the API by doing this which worked:
appKey<- 'XXXXXXXXXXXX'
appSecret<- 'YYYYYYYYY'
response = POST(
'https://accounts.facebook.com/api/token',
accept_json(),
authenticate(appKey, appSecret),
body = list(grant_type = 'client_credentials'),
encode = 'form',
verbose()
)
token = content(response)$access_token
HeaderValue = paste0('Bearer ', token)
From here, I want to call my account based on the fields provided here:
https://developers.facebook.com/docs/marketing-api/insights/fields/v2.11
My attempt to do this is as follows:
accountname<-'1234567'
account_url <- "https://api.facebook.com/v2.11/account_name/1234567"
Account <- GET(account_url, add_headers(Authorization = HeaderValue))
When I run Account, I get this output:
Response [https://www.facebook.com/unsupportedbrowser]
Date: 2018-01-05 21:39
Status: 200
Content-Type: text/html; charset=UTF-8
Size: 309 kB
<!DOCTYPE html>
<html lang="en" id="facebook" class="no_js">
<head><meta charset="utf-8" /><meta name="referrer" content="default"
id="meta_referrer" /><script>window._cstart=+new Date();</script>...
<link type="text/css" rel="stylesheet"
href="https://static.xx.fbcdn.net/rsrc.php/v3/yl/l/0,cross/Ms7B4ehALEW.css"
data-bootloader-hash...
<link type="text/css" rel="stylesheet"
href="https://static.xx.fbcdn.net/rsrc.php/v3/y3/l/0,cross/684seag7DTg.css"
data-bootloader-hash...
<link type="text/css" rel="stylesheet"
href="https://static.xx.fbcdn.net/rsrc.php/v3/yK/l/0,cross/g8YvVQ4GaAa.css"
data-bootloader-hash...
<link type="text/css" rel="stylesheet"
href="https://static.xx.fbcdn.net/rsrc.php/v3/yY/l/0,cross/423Tvkgz0LR.css"
data-bootloader-hash...
<link type="text/css" rel="stylesheet"
href="https://static.xx.fbcdn.net/rsrc.php/v3/y1/l/0,cross/leqp1EQMeCH.css"
data-bootloader-hash...
<script src="https://static.xx.fbcdn.net/rsrc.php/v3/yC/r/3-Z0DsUATk-.js"
data-bootloader-hash="+/Mwn" crossorigin="anonymous"></script>
<script>requireLazy(["ix"],function(ix){ix.add({"125923":
{"sprited":true,"spriteCssClass":"sx_4ccccf","spriteMapCssClass":"sp_lM6-
CJgzF...
...
I then try and convert it from json but get an error.
Do you think this is the best way to access FB Business Manager's API for Marketing or should I go a different route? I already tried this method but it didn't work for me: https://github.com/daroczig/fbRads
Any help would be great, thanks!

google api distance matrix into R

I have calculated the distances between cities through the google api service.
I need to get the distances into a distance matrix in R, but when I use the code:
de1 <- "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&destinations=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&mode=driving&language=en&sensor=false"
webpage1 <- getURL(de1)
webpage1 <- readLines(tc <- textConnection(webpage1)); close(tc)
pagetree <- htmlTreeParse(webpage1, error=function(...){})
If I then type "pagetree" I get an error message.
> pagetree
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}#media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
</style>
<img src="//www.google.com/images/errors/logo_sm.gif" alt="Google">
</head>
<body>
<p><b>400.</b> <ins>That’s an error.</ins>
</p>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins></p>
</body>
</html>
Can somebody help me out?
Thank you,
sofia p.
try
require(RCurl)
webpage1 <- getURL(URLencode(de1))
also the data that is returned is in JSON format
require(RJSONIO)
data<-fromJSON(webpage1)
maybe useful
if you want xml returned try
require(XML)
de2 <- "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&destinations=Berlin|Hamburg|Muenchen|Koeln|Frankfurt am Mein|Duesseldorf|Bremen|Hannover|Nuernberg&mode=driving&language=en&sensor=false"
webpage2 <- getURL(URLencode(de2))
data2<-xmlParse(webpage2)
try package ggmap
library(ggmap)
mapdist(from, to, mode = c("driving", "walking", "bicycling"),
output = c("simple", "all"), messaging = FALSE, sensor = FALSE,
language = "en-EN", override_limit = FALSE)

Resources