Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found two mistakes in my code, the first it was authenticating against Google plus while I should authenticate against Blogger.</p> <pre><code>&lt;?php $url = "https://accounts.google.com/o/oauth2/auth"; $params = array( "response_type" =&gt; "code", "client_id" =&gt; "XXXXXXXXXX.apps.googleusercontent.com", "redirect_uri" =&gt; "http://localhost/phpBlogger/oauth2callback.php", "scope" =&gt; "https://www.googleapis.com/auth/blogger"); // Request Blogger authentication $request_to = $url.'?'.http_build_query($params); header("Location: ".$request_to); ?&gt; if(isset($_GET['code'])) { // Get an access token $code = $_GET['code']; $url = 'https://accounts.google.com/o/oauth2/token'; $apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXX'; $blogId = 'XXXXXXXXXXXXXXXXXXXXXXXXX'; $params = array( "code" =&gt; $code, "client_id" =&gt; urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"), "client_secret" =&gt; urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX"), "redirect_uri" =&gt; urlencode("http://localhost/phpBlogger/oauth2callback.php"), "grant_type" =&gt; urlencode("authorization_code") ); // HTTP query builder foreach($params as $key=&gt;$value) { $fields .= $key.'='.$value.'&amp;'; } rtrim($fields, '&amp;'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $data = curl_exec($ch); curl_close($ch); //echo curl_errno($ch); $response = json_decode($data); $accessToken = $response-&gt;access_token; } </code></pre> <p>The second error was how I did construct the JSON POST query, I should add the Content-length to the header:</p> <pre><code> $postTitle = 'post test'; $postContent = 'just another test.'; $url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogId.'/posts/'; $body = ' { "kind": "blogger#post", "blog": {"id": "'.$blogId.'"}, "title": "'.$postTitle.'", "content": "'.$postContent.'" }'; $headerQuery = array(); $headerQuery[] = 'Authorization: OAuth '.$accessToken; $headerQuery[] = 'Content-Length: '.strlen($body); $headerQuery[] = 'Content-Type: application/json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerQuery); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); $data = curl_exec($ch); /*var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT)); echo "&lt;br&gt;&lt;br&gt;&lt;br&gt;".$data;*/ //echo curl_errno($ch); $response = json_decode($data); echo "url: " . $response-&gt;url."&lt;br /&gt;"; echo "id: " . $response-&gt;id."&lt;br /&gt;"; curl_close($ch); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload