Reputation: 873
I'm trying to access Blogger via the JavaScript V3 API. Everything works as expected if I try to access my (public) test blog.
If I use the same code but try to access my (private) test blog I get an error.
The following is my code
var client_id = ['WWWW', 'XXXX'];
var api_key = ['YYYY','ZZZZ'];
var discoveryDocs = ['https://www.googleapis.com/discovery/v1/apis/blogger/v3/rest'];
var scope = 'https://www.googleapis.com/auth/blogger.readonly';
var blog_id = ['BLOG0', 'BLOG1'];
var appendResults = function(results) {
$('#results').append(JSON.stringify(results, undefined, 2) + '<hr/>');
};
getBlogs = function(client, key, blog) {
gapi.client.init({
'apiKey': key,
'clientId': client,
'discoveryDocs': discoveryDocs,
'scope': scope
}).then(function() {
return gapi.client.blogger.posts.list({
'blogId': blog
});
}).then(function(d) {
return d;
}).then(function(response) {
appendResults(response);
}, function(reason) {
appendResults(reason);
});
};
gapi.load('client', function() {
for(i=0; i<api_key.length; i++) {
getBlogs(client_id[i], api_key[i], blog_id[i]);
}
});
Where the second element in api_key and blog_id is my private blog.
The following is my response
{
"result": {
"kind": "blogger#postList",
"items": [
{
"kind": "blogger#post",
"id": "XXXX",
"blog": {
"id": "XXXX"
},
"published": "XXXX",
"updated": "XXXX",
"etag": "\"XXXX\"",
"url": "http://XXXX/2017/03/blog-post.html",
"selfLink": "https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX",
"title": "",
"content": "XXXX",
"author": {
"id": "XXXX",
"displayName": "XXXX",
"url": "https://www.blogger.com/profile/XXXX",
"image": {
"url": "XXXX"
}
},
"replies": {
"totalItems": "0",
"selfLink": "XXXX"
}
}
],
"etag": "\"XXXX\""
},
"body": "{\n \"kind\": \"blogger#postList\",\n \"items\": [\n {\n \"kind\": \"blogger#post\",\n \"id\": \"XXXX\",\n \"blog\": {\n \"id\": \"639440130428294175\"\n },\n \"published\": \"XXXX\",\n \"updated\": \"XXXX\",\n \"etag\": \"\\\"XXXX\\\"\",\n \"url\": \"http://XXXX/2017/03/blog-post.html\",\n \"selfLink\": \"https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX\",\n \"title\": \"\",\n \"content\": \"XXXX\",\n \"author\": {\n \"id\": \"XXXX\",\n \"displayName\": \"XXXX\",\n \"url\": \"https://www.blogger.com/profile/XXXX\",\n \"image\": {\n \"url\": \"XXXX\"\n }\n },\n \"replies\": {\n \"totalItems\": \"0\",\n \"selfLink\": \"https://www.googleapis.com/blogger/v3/blogs/XXXX/posts/XXXX/comments\"\n }\n }\n ],\n \"etag\": \"\\\"XXXX\\\"\"\n}\n",
"headers": {
"date": "XXXX",
"content-encoding": "gzip",
"vary": "Origin, X-Origin",
"content-length": "576",
"pragma": "no-cache",
"server": "GSE",
"etag": "\"XXXX\"",
"content-type": "application/json; charset=UTF-8",
"cache-control": "no-cache, no-store, max-age=0, must-revalidate",
"expires": "XXXX"
},
"status": 200,
"statusText": null
}
and
{
"result": {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "We're sorry, but the requested resource could not be found."
}
],
"code": 400,
"message": "We're sorry, but the requested resource could not be found."
}
},
"body": "{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"invalid\",\n \"message\": \"We're sorry, but the requested resource could not be found.\"\n }\n ],\n \"code\": 400,\n \"message\": \"We're sorry, but the requested resource could not be found.\"\n }\n}\n",
"headers": {
"date": "XXXX",
"content-encoding": "gzip",
"vary": "Origin, X-Origin",
"content-length": "160",
"server": "GSE",
"content-type": "application/json; charset=UTF-8",
"cache-control": "private, max-age=0",
"expires": "XXXX"
},
"status": 400,
"statusText": null
}
I've setup credentials in the developer console. I'm accessing this via localhost:5000/ which I've setup localhost as a valid domain in the developer console.
The only thing I can think that might be a clue is
"result": {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "We're sorry, but the requested resource could not be found."
}
],
"code": 400,
"message": "We're sorry, but the requested resource could not be found."
}
This might be telling me that my requesting origin is invalid but I can't say for sure.
If I turn the same (private) blog public and re-execute, then this code works for both blog ID's.
Where am I going wrong?
Upvotes: 2
Views: 1291
Reputation: 5651
Blogger API requires that your application has OAuth 2.0 token when accessing information on a private blog (Refer to the documentation here). Only utilizing the API key won't make the request work.
Authorization is required if the posts are on a blog that is private. If the posts are on a blog that is public, then this method can be called without authorization.
Also, the scope is different when using OAuth tokens -
Here's the OAuth 2.0 scope information for the Blogger API:
https://www.googleapis.com/auth/blogger
To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies during application registration (such as the client ID and/or the client secret).
You can test this API via https://developers.google.com/apis-explorer/#search/blogger.posts.list/m/blogger/v3/blogger.posts.list
Upvotes: 0