In my Android application, I want to upload image to the Blobstore, then retrieve an Upload url and the image\'s Blobkey, so I can store the Blobkey in the DataStore.
I am working with endpoints in Android Studio, thanks to SAVANTE, I can finish my code but I had to make small adjustments.
in Servlet 1: I used Endpoints, with this I can handle very easy the OAuth2 in my method:
@ApiMethod(name = "getBlobURL", scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID,
Constants.ANDROID_CLIENT_ID,
com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
public BlobAttributes getBlobURL(User user) throws UnauthorizedException,
ConflictException{
//If if is not null, then check if it exists. If yes, throw an Exception
//that it is already present
if (user == null){
throw new UnauthorizedException("User is Not Valid");
}
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String blobUploadUrl = blobstoreService.createUploadUrl("/blobupload");
//BlobAttributes is a class
BlobAttributes ba= new BlobAttributes();
ba.setBlobURL(blobUploadUrl);
return ba;
}
My Backend in endpoints Android Studio, do not let me use JSONObject for this rason I make my own Json: in Servlet 2:
String myJson = "{'servingUrl': '" + servingUrl +
"', 'blobKey': '" + blobKey.getKeyString() + "'}";
PrintWriter out = resp.getWriter();
out.print(myJson);
out.flush();
out.close();
I hope works for somebody else, I spent 48 hours trying to understand and operate Blobstore.
Edit:
For make a authenticated call from client this is the way using Google credentials:
accountName = settings.getString(start.KEY_ACCOUNT_NAME, null); //Email account that you before save it
credential = GoogleAccountCredential.usingAudience(getActivity(),
start.WEB_CLIENT_ID); //WEB_CLIENT_ID is your WEB ID in Google Console
credential.setSelectedAccountName(accountName);
When build your Endpoint put your credential:
PostEndpoint.Builder builder = new PostEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential)
.setRootUrl(getActivity().getString(R.string.backend_url_connection));
myApiService = builder.build();
For Get the Account Name of the client, use Plus API
accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
Read the links down in the comments, with Google documentation for good understand this.