Accessing Books API through AppEngine fails with error code 400 - java

I'm getting following error when accessing Google Books APIs from Google AppEngine Application.
API key for server application is used.
But if you run application locally in eclipse there is no problem.
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Cannot determine user location for geographically restricted operation.",
"reason" : "unknownLocation"
} ],
"message" : "Cannot determine user location for geographically restricted operation."
}
There is not enough information on this error scenario.
Any help is appreciated. Thanks.

Well, this may be because the IP cannot be used to locate the user. It makes sense looking at the error message and with some googling here:
https://productforums.google.com/forum/#!topic/books-api/88Ml3YIpvLw
Try adding &country=GB to the end of the request, or whichever 2 letter represent the country which you are wanting to search from. (More here: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
This answer is mainly from the answer in the given link, however it appears to work and took some looking for. I hope it helps.

This following code solved my problem using Google Books API itself.
///////////////////////////////////////////////////
UrlFetchTransport url = new UrlFetchTransport();
final Books books = new Books.Builder(
url, jsonFactory, null)
.setApplicationName(APPLICATION_NAME)
.setGoogleClientRequestInitializer(
new GBookRequest()).build();
List volumesList = books.volumes().list("isbn:9780199562855");
// Execute the query.
Volumes volumes = volumesList.execute();
if (volumes.getTotalItems() == 0 || volumes.getItems() == null) {
log.info("No matches found in GBooks.");
return null;
}
///////////////////////////////////////////////////
public class GBookRequest extends BooksRequestInitializer {
private static String apiKey = "xxxxxx";
public GBookRequest() {
super(apiKey);
}
#Override
public void initializeBooksRequest(BooksRequest request)
throws IOException {
request.set("country", "US");
}
}
///////////////////////////////////////////////////

Related

How to get Google Ads impressions based on location

I am working with Google Ads API. Based on my use case, I need to get the impressions, clicks, and other statistics about the products based on location(Without creating the campaign).
I contacted the Google Ads API team to address the issue but the doc reference they provided me didn't work in my case. (I'm not using keywords also)
generate forecast metrics
And also the query I used to access the data is...
public void queryTest(long customerId,String campaignId) {
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
SearchGoogleAdsStreamRequest request =
SearchGoogleAdsStreamRequest.newBuilder()
.setCustomerId(Long.toString(customerId))
.setQuery("SELECT metrics.clicks, metrics.impressions,geographic_view.country_criterion_id FROM geographic_view WHERE geographic_view.country_criterion_id = 1009919")
.build();
ServerStream<SearchGoogleAdsStreamResponse> stream =
googleAdsServiceClient.searchStreamCallable().call(request);
for (SearchGoogleAdsStreamResponse response : stream) {
for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
System.out.println("outPut"+googleAdsRow.getGeographicView());
}
}
}
}
Can someone please help me to resolve this issue??
Thank you!!

Firebase Database security rules works in simulator, not in the java code

I want to block addition of data with timestamps older than existing ones. Sample database:
{
"latest" : {
"517" : {
"PARAM1" : {
"timestamp" : 11492,
"value" : 6593
}
}
}
}
Firebase security rules:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"latest": {
"$id": {
"$param": {
"timestamp": { ".validate": "newData.val() >= data.val()" }
}
}
}
}
}
Now, interesting part. When I check adding different data from the simulator - it works as expected. Smaler timestamps are rejected, higher are added. But it doesn't make any impact on adding THE SAME data from the Java code.
Sample data which should be rejected:
/latest/517
{
"PARAM1" : {
"timestamp" : -5,
"value" : 643
}
}
I use com.google.firebase:firebase-admin:4.0.3 with pure Java as described in Admin Database API section.
POJO:
public class TimeValue {
public long timestamp;
public double value;
public TimeValue(long timestamp, double value) {
super();
this.timestamp = timestamp;
this.value = value;
}
}
Inserting:
Map<String, TimeValue> timeValue = new HashMap<String, TimeValue>();
timeValue.put("PARAM1", new TimeValue(-5L, 643));
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference idRef = database.getReference("latest/517");
idRef.setValue(timeValue);
Why it works that way? It doesn't make any sense.
When you use firebase as admin, or as a service account, the validation rules are not checked -- because, well, you're an admin :)
If you just want normal access to the database, you can just sign in as a regular account.
If you want to have admin privileges, but still want the validation rules checked, you just need to use a "special" account that will always satisfy the read/write rules. I stumbled upon this too some time ago, there are two basic ways to do it, explained in my question (apparently equally safe, as this was my question back then):
Use databaseAuthVariableOverride to set some special auth property, which is checked in root .read/.write rules, and is true only for the service account.
Create a special account (even a plain email/password), and hardcode its uid, email or other property in the same rules (root's .read/.write).

Get all videos of a YouTube Playlist in Java

Context I'm working with Android Studio (Java). I want to obtain all the videos of a given playlist (or 50, I will get all the other after).
Problem I see people using url like
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLiEwZfNgb4fVrRzTonlVEMj6DB2Nmzg2M&key=AIzaSyC2_YRcTE9916fsmA0_KRnef43GbLzz8m0
but I don't know how to implement this in Java. I follow some tuto and I got a way to get information totally different like :
YouTube.Search.List query;
query = youtube.search().list("id,snippet");
query.setKey(MY_API_KEY);
query.setMaxResults((long)20);
query.setType("video");
query.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url)");
And I really don't understand how do I get something else than a search.
Most of documentation is in english only...
EDIT
Ok, so I continued to try, I think I got a near solution, but I got an error.
private YouTube youtube;
private YouTube.PlaylistItems.List playlistItemRequest;
private String PLAYLIST_ID = "PLiEwZfNgb4fVrRzTonlVEMj6DB2Nmzg2M";
public static final String KEY = "AIzaSyC2_YRcTE9916fsmA0_KRnef43GbLzz8m0";
// Constructor
public YoutubeConnector(Context context)
{
youtube = new YouTube.Builder(new NetHttpTransport(),
new JacksonFactory(), new HttpRequestInitializer()
{
#Override
public void initialize(HttpRequest hr) throws IOException {}
}).setApplicationName(context.getString(R.string.app_name)).build();
}
public List<VideoItem> result()
{
List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
try
{
/* HERE MUST BE MY PROBLEM ! */
playlistItemRequest = youtube.playlistItems().list("snippet");
playlistItemRequest.setPlaylistId(PLAYLIST_ID);
playlistItemRequest.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url),nextPageToken,pageInfo");
playlistItemRequest.setKey(KEY);
String nextToken = "";
do {
playlistItemRequest.setPageToken(nextToken);
PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
playlistItemList.addAll(playlistItemResult.getItems());
nextToken = playlistItemResult.getNextPageToken();
} while (nextToken != null);
}catch(IOException e)
{
Log.d("YC", "Could not initialize: "+e);
}
//[...]
}
Here is the error I got :
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"location" : "fields",
"locationType" : "parameter",
"message" : "Invalid field selection videoId",
"reason" : "invalidParameter"
} ],
"message" : "Invalid field selection videoId"
}
EDIT 2 Thanks to : Martijn Woudstra.
Correct line was :
playlistItemRequest = youtube.playlistItems().list("snippet,contentDetails");
//[...]
playlistItemRequest.setFields("items(snippet/title,snippet/description,snippet/thumbnails/default/url,contentDetails/videoId),nextPageToken,pageInfo");
//[...]
videoItem.setId(item.getContentDetails().getVideoId());
I know that is an old question but It is important to identify which resources are we using to understand how to get the proper information. There are many resources in the YouTube API v3, but we usually use the search, video, playlist and playlistItems.
According to the documentation the following JSON structure shows the format of a playlistItems resource:
{
"kind": "youtube#playlistItem",
"etag": etag,
"id": string,
"snippet": {
"publishedAt": datetime,
"channelId": string,
"title": string,
"description": string,
"thumbnails": {
(key): {
"url": string,
"width": unsigned integer,
"height": unsigned integer
}
},
"channelTitle": string,
"playlistId": string,
"position": unsigned integer,
"resourceId": {
"kind": string,
"videoId": string,
}
},
"contentDetails": {
"videoId": string,
"startAt": string,
"endAt": string,
"note": string,
"videoPublishedAt": datetime
},
"status": {
"privacyStatus": string
}
}
From this structure, we may suppose that there are three ways to get the videoId. But first it is important to know how we going to define the PARTS and the FIELDS of the resource.
To define the PARTS we use this code:
YouTube.PlaylistItems.List list = youtube.playlistItems().list("snippet");
In the previous line, "snippet" identifies a property that contains numerous fields (or child properties), including the title, description, position, and resourceId, so when we set "snippet" the API's response will contain all of those child properties.
Now, we also can limit the previous properties if we define the FIELDS. For example, in this code:
list.setFields("items(id/videoId,snippet/title,snippet/description," +
"snippet/thumbnails/default/url)");
If we call list.execute(), it will show an error because we didn't define id in the PARTS properties. Also, according to the JSON structure, id is a String and does not contains videoId as a child property. Ah!, but we can extract videoId from the resourceId? -Well, the answers is YES/NO. -Why so? Come on Teo, the JSON structure shows it clearly. -Yes, I can see that, but the documentation says:
If the snippet.resourceId.kind property's value is youtube#video, then this property will be present and its value will contain the ID that YouTube uses to uniquely identify the video in the playlist.
This means that sometimes may not be available. -Then, how we can get the videoId? -Well, we can add id or contentDetails to the PARTS resources. If we add id then defines fields like this:
YouTube.PlaylistItems.List list = youtube.playlistItems().list("id,snippet");
list.setFields("items(id,snippet/title,snippet/description," +
"snippet/thumbnails/default/url)");
If we add contentDetails then defines fields like this:
YouTube.PlaylistItems.List list = youtube.playlistItems()
.list("snippet,contentDetails");
list.setFields("items(contentDetails/videoId,snippet/title,snippet/description," +
"snippet/thumbnails/default/url)");
I hope this helps you guys.
id/videoId doesnt exist.
There is an id and a snippet/resourceId/videoId.
So my guess is your setfields aren't right.

Google app-engine updating user information getting error 400 BAD_REQUEST

While updating user information using Directory API of Admin SDK getting an error :
400 BAD_REQUEST
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Input: Bad request for ",
"reason" : "invalid"
} ],
"message" : "Invalid Input: Bad request for "
}
Trying to update organizations details for user the fields like name, title and department
My sample code : `
Get users = directoryService.users().get(userEmail);
User user = users.execute();
try{
List<UserOrganization> userOrg = new ArrayList<UserOrganization>();
userOrg = user.getOrganizations();
if(userOrg != null){
UserOrganization f_userOrg = new UserOrganization();
f_userOrg = userOrg.get(0);
if(f_userOrg != null){
f_userOrg.setTitle("SAP Asso");
f_userOrg.setName("xyz company name");
f_userOrg.setDepartment("xyz dept name");
f_userOrg.setType("work");
userOrg.add(f_userOrg);
user.setOrganizations(userOrg);
}
}
InputStream body = directoryService.users().update(userEmail,user).executeAsInputStream();
// # this line it throws exception 400 BAD_REQUEST
}catch(Exception e){
e.printStackTrace();
}
I refer this update_user link for updating user data.
Any Help will be appreciated.
Thanks.
Can you print the request that you are sending to Google API. There can be a problem with the format that you are sending.
400 BAD_REQUEST is the request could not be understood by the server
due to malformed syntax. The client SHOULD NOT repeat the request
without modifications.
So basically there is mismatch of parameter while API call. In this case your request goes to the server but due to wrong request parameter it gives 400 error.

View all comments on a YouTube video

I am trying to get all the comments on a YouTube video using a Java program. I cannot get them though as it has the "Show More" instead of all the comments. I'm looking for a way to get all the comments or pages of comments that I can go through. I have a video id and things, just need the comments.
I have tried all_comments instead of watch in the URL but it doesn't show all comments still and redirects to watch again.
I have also looked at the YouTube api and can only find how to get comments with their id but I need to get all comments from a video id.
If anyone knows how to do this please tell me.
I have added a 50 rep bounty for whoever can give me a good answer to this.
You need to get comment threads list request for your video and then scroll forward using next page token from the last response:
private static int counter = 0;
private static YouTube youtube;
public static void main(String[] args) throws Exception {
// For Auth details consider:
// https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/Auth.java
// Also don't forget secrets https://github.com/youtube/api-samples/blob/master/java/src/main/resources/client_secrets.json
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
Credential credential = Auth.authorize(scopes, "commentthreads");
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).build();
String videoId = "video_id";
// Get video comments threads
CommentThreadListResponse commentsPage = prepareListRequest(videoId).execute();
while (true) {
handleCommentsThreads(commentsPage.getItems());
String nextPageToken = commentsPage.getNextPageToken();
if (nextPageToken == null)
break;
// Get next page of video comments threads
commentsPage = prepareListRequest(videoId).setPageToken(nextPageToken).execute();
}
System.out.println("Total: " + counter);
}
private static YouTube.CommentThreads.List prepareListRequest(String videoId) throws Exception {
return youtube.commentThreads()
.list("snippet,replies")
.setVideoId(videoId)
.setMaxResults(100L)
.setModerationStatus("published")
.setTextFormat("plainText");
}
private static void handleCommentsThreads(List<CommentThread> commentThreads) {
for (CommentThread commentThread : commentThreads) {
List<Comment> comments = Lists.newArrayList();
comments.add(commentThread.getSnippet().getTopLevelComment());
CommentThreadReplies replies = commentThread.getReplies();
if (replies != null)
comments.addAll(replies.getComments());
System.out.println("Found " + comments.size() + " comments.");
// Do your comments logic here
counter += comments.size();
}
}
Consider api-samples, if you need a sample skeleton project.
Update
The situation when you can't get all the comments can be also caused by the quota limits (at least I faced it):
units/day 50,000,000
units/100seconds/user 300,000
This is not a java, python, js, or whatever language specific rules. If you want to get above the quota, you cant try to apply for higher quota. Though, I would start from controlling your throughput. It's very easy to get above the 100seconds/user quota.
try this it can download all the comments for a given video which i have tested.
https://github.com/egbertbouman/youtube-comment-downloader
python downloader.py --youtubeid YcZkCnPs45s --output OUT
Downloading Youtube comments for video: YcZkCnPs45s
Downloaded 1170 comment(s)
Done!
output is in the JSON format:
{
"text": "+Tony Northrup many thanks for the prompt reply - I'll try that.",
"time": "1 day ago",
"cid": "z13nfbog0ovqyntk322txzjamuensvpch.1455717946638546"
}

Categories

Resources