Skip to content

Instantly share code, notes, and snippets.

@sblendorio
Last active July 20, 2021 16:29
Show Gist options
  • Save sblendorio/30d518dca9b84b597d4b008e1f076a6b to your computer and use it in GitHub Desktop.
Save sblendorio/30d518dca9b84b597d4b008e1f076a6b to your computer and use it in GitHub Desktop.
Google - Service Account Usage (Example: read from a Blogger site)
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.blogger.Blogger;
import com.google.api.services.blogger.BloggerScopes;
import com.google.api.services.blogger.model.Blog;
import com.google.api.services.blogger.model.Post;
import com.google.api.services.blogger.model.PostList;
import java.io.FileInputStream;
import java.util.Collections;
public class BloggerReadPostList {
public static void main(String[] args) throws Exception {
final String BLOG_URL = "http://attivissimo.blogspot.com";
final String CRED_FILE_PATH = System.getProperty("user.home") + "/credentials.json";
final String APPLICATION_NAME = "SampleBloggerReading";
final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
// Specify "Authorization Scope" [BloggerScopes.BLOGGER = "https://www.googleapis.com/auth/blogger"]
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream(CRED_FILE_PATH))
.createScoped(Collections.singleton(BloggerScopes.BLOGGER));
Blogger blogger = new Blogger.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
String blogId = blogger.blogs().getByUrl(BLOG_URL).execute().getId();
// Start from first page
Blogger.Posts.List action = blogger.posts().list(blogId).setPageToken(null);
// Restrict the result content to just the data we need.
action.setFields("items(author/displayName,id,content,published,title,url),nextPageToken");
action.setMaxResults(10L);
// This step sends the request to the server.
PostList list = action.execute();
for (Post post: list.getItems()) System.out.println("[" + post.getId() + "] " + post.getTitle());
System.out.println("Next token = " + list.getNextPageToken());
}
}
{
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "..."
}
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-blogger</artifactId>
<version>v3-rev61-1.25.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.39.2</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment