Adding a post using Blogger API not working

641 Views Asked by At

i am using Google Blogger API to create posts programmatically in a blog I created. I am using the code below to try and create a new post but it is not adding a new blog

The code:

private void addPost(){
new Thread (()->{
String mUserId = "MY_BLOGGER_POST_ID_HERE";
          String mYApiKey = "MY_API_KEY";
          String Authorization = "SOME_CHARACTERS_AND.apps.googleusercontent.com_HERE";

            

              final JSONObject obj = new JSONObject();
              obj.put("id", mUserId);

              final JSONObject requestBody = new JSONObject();
              requestBody.put("kind", "blogger#post");
              requestBody.put("blog", obj);
              requestBody.put("title", "TestingTheBlogOneTwo");
              requestBody.put("content", "ContentExample");

              DefaultHttpClient httpClient = new DefaultHttpClient();
              final HttpPost request = new HttpPost("https://www.googleapis.com/blogger/v3/blogs/" + mUserId + "/posts?key=" + mYApiKey + "");
              request.addHeader("Authorization", Authorization);
              request.addHeader("Content-Type", "application/json");
              request.setEntity(new StringEntity(requestBody.toString()));
    
              HttpResponse response = httpClient.execute(request);
              if (response.getStatusLine().getStatusCode() != 200){
                  Toast.makeText(getApplicationContext(), "status code " + response.getStatusLine().getStatusCode(), Toast.LENGTH_SHORT).show();
              }
              else {
                  Toast.makeText(getApplicationContext(), "done: ", Toast.LENGTH_SHORT).show();

              }
}).start();
}

But it's not adding any post

•Is this the right way to add a post using Blogger Api?

•Are my omitting anything?

*Please what are my getting wrong.

Any help will be appreciated will be greatly appreciated. Thanks in advance.

Update: The app is not crashing and no new blog is being added

1

There are 1 best solutions below

2
dominicoder On

•Is this method correct?

To know if this method is "correct" we would have to know what your definition of "correct" is. Since you've added no information beyond "here's a function", we cannot answer this question.

Cause it's returning null.

No, it's not. You've defined this as:

Private void addPost(){

Ignoring the fact that Private (with a capital P) is not a thing, your method return type is void - which means it returns nothing. Thus, by definition, it cannot be "returning null".


Edit:

•Is this the right way to add a post using Blogger Api?

Have you read through the Blogger API documentation and done everything as it explains?

If so, then yes. If not, then no.

Are my omitting anything?

Not sure how anyone is supposed to know if you're omitting anything.

Buddy - what is your actual problem? "Am I doing this right" or "Am I missing anything" are not good questions. What is happening to cause you to come to SO with this question? Is there an error? Is there a crash? Does your laptop blow up?