I try to send a Post
to Blogger
using the one Api v3
from Blogger
Y Php
. The code works fine but I have some doubts about how to update the token without giving permissions again, I thought with this part:
$client->setAccessType('offline');
I didn't need to update it.
Another question is why I can't send an image using this way:
$post->setimages(array("url"=>"https://image.freepik.com/vector-gratis/casa-dos-pisos_1308-16176.jpg"));
The published image does not appear. According to this link Posts: insert that is the way or so I understand.
I leave you the complete code so you can understand and help me.
require_once __DIR__.'/vendor/autoload.php';
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setApplicationName('B'); //name of the application
$client->setAuthConfig('client_secret.json');
$client->setRedirectUri($scriptUri); //redirects to same url
$client->setApprovalPrompt("force");
$client->setAccessType('offline'); // default: offline
$client->setApprovalPrompt("consent");
$client->addScope(Google_Service_Blogger::BLOGGER);
$client->setIncludeGrantedScopes(true); // incremental auth
$guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
$client->setHttpClient($guzzleClient);
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
//authenticate using the parameter $_GET['code'] you got from google server
$client->authenticate($_GET['code']);
//get the access token
$tokens = $client->getAccessToken();
file_put_contents(__DIR__."/Data/token.json", $tokens);
}
$service = new Google_Service_Blogger($client);
$blog = $service->blogs->getByUrl('http://Miblog.mx/');
//this is part for sending post into blogger, which is getting
//error
$post = new Google_Service_Blogger_Post();
$post->setTitle('poner imagen 3');
$post->setImages(array("url"=>"https://image.freepik.com/vector-gratis/casa-dos-pisos_1308-16176.jpg"));
$post->setLabels(array('Label1', 'Label2'));
$post->setContent("plantilla.html");
$service->posts->insert('203422', $post);
This part also doesn't work as I expect.
$post->setContent("plantilla.html");
It does not publish the html of "plantilla.html"
and I do not know why or how it is achieved and I have searched the Internet and there is not much information in Spanish
I will answer each of the questions in a section.
With
setAccessType('offline')
I don't need to refresh the tokenWe are dealing with OAuth tokens, so as you can read in the documentation , the mode
offline
allows the application to refresh the token without user interaction: without the user being present in a browser or re-validating the tokens. permissions.But the tokens always have an expiration time and even a maximum renewal time. With this option, you delegate the work to the PHP client, but if the renewal terms of the token are exceeded, the user will have to request access again.
You can see a token refresh procedure in any example of the repository
php-samples
. I show the example of access to the calendar :Another question is why I can't send an image using this way
I have tried the Blogger API and it is not possible to add an image to a post, neither external (like the one you indicated) nor previously uploaded with the image upload tool (URLs start with
https://4.bp.blogspot.com/
) nor from the personal album of the author of the post (URLs start withhttps://lh3.googleusercontent.com/
).Although in the documentation it says:
In practice, even using the Try this API function , I have not been able to insert any image as a display image (although I have been able to put an image to display by adding an image inside the content) and I have not been able to obtain any results search with the index
images
except in the icon of the user who is the author of a post or a comment.I believe that this field is used when querying data from a publication that does have an image to display, not to change the image to display to the one we want.
$post->setContent("plantilla.html")
it doesn't work as i expectThis part is the easiest to clear. The function parameter is the post content, not a file containing the post content.
So you should load the content of the file using, for example,
file_get_contents()
: