Using Java blogger API v3 to blog dynamically

I have two problems regarding using the Java blogger API v3 to dynamically post messages to my blogger account.

The first

I used the following code to get credentials to access my blog:

GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)                
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(emailAddress)
                .setServiceAccountPrivateKeyFromP12File(
                new File(p12FileLocation))
                .setServiceAccountScopes(Collections.singleton(BloggerScopes.BLOGGER))
                .build();

        credential.setAccessToken("zRLqmkM82626Uym9Uv1Jsdd");


        Blogger blogger = new Blogger.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName("Blogger")
                .build();
        // .... rest of the code to prepare post and send it ......

I set the access token above (credential.setAccessToken), which was created on the following google page: https://developers.google.com/oauthplayground

but this token expires every 3600 seconds. so I went back to the page and click the "Update access token" button to get another one and use it again in the above code.

Is this the right way to access my blog and dynamically publish content and articles programmatically?

Second

Google https://developers.google.com/console , 10000 / 1 //

50 ( , 5 ), api:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Rate Limit Exceeded",
    "reason" : "rateLimitExceeded"
  } ],
  "message" : "Rate Limit Exceeded"
}

, , , , !!!

- :

?

.

+2
2

- Blogger, , Blogger Api - Auth 2 Playground, API.

, 10000 / 1 // , , Blogger api 50 . , , .

+1

api , . .

private static GoogleCredential getCredentials(HttpTransport httpTransport, JacksonFactory jacksonFactory,
        List<String> scopes) throws IOException, GeneralSecurityException {
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jacksonFactory,
            CLIENT_ID, CLIENT_SECRET, scopes).setAccessType("online").setApprovalPrompt("auto").build();
    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    System.out.println("Please open the following URL in your " + "browser then type the authorization code:");
    System.out.println("  " + url);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String code = br.readLine();
    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
    System.out.println("Response : " + response.toPrettyString());
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jacksonFactory).setServiceAccountId("xyz@gmail.com")
            .setServiceAccountPrivateKeyFromP12File(new File("resources\\xyz.p12"))
            .setServiceAccountScopes(Collections.singleton(BloggerScopes.BLOGGER)).build();
    credential.setAccessToken(response.getAccessToken());
    return credential;
}

public static Blogger getBlog() throws IOException, GeneralSecurityException, AuthenticationException {
    if (blog == null) {
        if (httpTransport == null)
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        if (jacksonFactory == null)
            jacksonFactory = JacksonFactory.getDefaultInstance();
        blog = new Blogger.Builder(httpTransport, jacksonFactory,
                getCredentials(httpTransport, jacksonFactory, Arrays.asList(BloggerScopes.BLOGGER)))
                        .setApplicationName("Blogger").build();
    }
    return blog;
}

public static void udpatePost(String title, String content) throws IOException, AuthenticationException, GeneralSecurityException{
    Post post = new Post();
    post.setTitle(title);
    post.setContent(content);
    Update updateAction = getBlog().posts().update(BLOG_ID, POST_ID, post);
    updateAction.setFields("author/displayName,content,published,title,url");
    post = updateAction.execute();
    System.out.println("Published: " + post.getPublished());  
}

JAR API v3: http://developers.google.com/blogger/docs/3.0/api-lib/java

+3

Source: https://habr.com/ru/post/1616655/


All Articles