自动在Blogspot上发布帖子

时间:2015-03-17 00:27:52

标签: php google-api google-oauth blogger blogspot

您好我正在尝试开发一个功能,可以在代码中发布我的博客,而无需网站查看器的任何授权或操作。

我需要发送两个参数,帖子标题和帖子内容。

我们可以分配那些$ title和$ content

我现在已经工作和研究了大约两个星期了。关于如何执行此操作的文档几乎没有,并且存在的文档已过时且非常难以使用。

任何帮助都会受到极大的赞赏。

我已经能够使用Blogger V3 API公钥正确配置我的API密钥。我已经获得了json数据的有效返回,但我只需要知道如何发帖。

谢谢!

<?php

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


$blogID = '2139447653782748476'; 
$apikey = '***';

$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";

//$requestURL = "http://icanhazip.com";

//$json = file_get_contents("http://icanhazip.com");

$json = file_get_contents_curl($requestURL);

$json = json_decode($json);

//echo $requestURL;

//var_dump($json);

var_dump($json);

//echo $json;

?>

我试过的第二个代码:

<?php
/*
function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


if ($_GET['key'] != 'buswell') {
die();
}
/*
$blogID = '2139447653782748476'; 
$apikey = 'AIzaSyDbouGxlQ9P9JMuH4SJlq43xMhyVGWe14g';

$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";

//$requestURL = "http://icanhazip.com";

//$json = file_get_contents("http://icanhazip.com");

$json = file_get_contents_curl($requestURL);

$json = json_decode($json);

//echo $requestURL;

//var_dump($json);

var_dump($json);

//echo $json;

*/

session_start();
require_once dirname(__FILE__).'/google-api-php-client-master/src/Google/Client.php';
require_once dirname(__FILE__).'/google-api-php-client-master/src/Google/Service/Client.php';

$scriptUri = "http://connectionincognito.com/dev.php";

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('giaws'); //name of the application
$client->setClientId('924143111807-63s8j0f8rnn7ps3pdtnah827rvj29mnr.apps.googleusercontent.com'); //insert your client id
$client->setClientSecret('77c9742276a14988db77f8b5454f9ba378762246'); //insert your client secret
$client->setRedirectUri($scriptUri); //redirects to same url
$client->setDeveloperKey('77c9742276a14988db77f8b5454f9ba378762246'); // API key (at bottom of page)
$client->setScopes(array('https://www.googleapis.com/auth/blogger')); //since we are going to use blogger services

$blogger = new Google_BloggerService($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
 die('Logged out.');
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}
//you can get the data about the blog by getByUrl
$data = $blogger->blogs->getByUrl(array('url'=>'http://proxies-unlimited.blogspot.com/'));

//creates a post object
$mypost = new Google_Post();
$mypost->setTitle('this is a test 1 title');
$mypost->setContent('this is a test 1 content');

$data = $blogger->posts->insert('2139447653782748476', $mypost); //post id needs here - put your blogger blog id
 var_dump($data);
?>

0 个答案:

没有答案