B4J Question Google Cloud Storage authentication

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

i want to use Google Cloud Storage for my image backend, my B4J Service would be handle the auth.
I have create a service account and have set the permissions to this account, to access the bucket.

How can i authenticate the B4J service to access the images?

I have a .json file with "project_id", "private_key_id", "private_key"...

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
The OAuth2 Class does work with B4J too
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Ohh, i see. that´s a problem for sure. Unfortunately i can´t help here.
I am using a serviceaccount on our website to sync our Calendar to Google Calendar using a php library from Google.

I don´t know the correct way to authenticate from java (non ui). I guess you need to build everything for a Google HTTP Client.
I tried to build one a few times but only had limited success so far.

https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-java

https://github.com/googleapis/google-api-java-client

But as written; i wasn´t successful until now.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Based on
https://cloud.google.com/datastore/docs/reference/libraries
you need to Setup your System environment to define the Path to the json.

Based on their Java example i build a small testlib.

you need to add the jars using #additionaljar
Make sure you successfully set the environment.

Don´t know if it works. I do not have a cloudstorage account which i could test.
 

Attachments

  • GoogleCloudStorageV0.0.zip
    275.6 KB · Views: 361
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    public void Initialize(BA ba, String EventName) {
        this.eventName = EventName.toLowerCase(BA.cul);
        this.ba = ba;
        Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
    // The kind for the new entity
    String kind = "Task";
    // The name/ID for the new entity
    String name = "sampletask1";
    // The Cloud Datastore key for the new entity
    Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);

    // Prepares the new entity
    Entity task = Entity.newBuilder(taskKey)
        .set("description", "Buy milk")
        .build();

    // Saves the entity
    datastore.put(task);
    ba.Log("Saved "+task.getKey().getName()+"/"+task.getString("description"));

    //Retrieve entity
    Entity retrieved = datastore.get(taskKey);

    ba.Log("Retrieved "+taskKey.getName()+" / "+retrieved.getString("description"));

    }
So, if all works (i do expect crashes :D) you should get two lines in your log and a Buy milk task should be created in the Datastore.

But i expect it to crash at this stage of development.

I´m interested in your answer ;-)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
The compiler said:
B4X:
javac 1.8.0_191
src\b4j\example\main.java:98: error: cannot access Datastorewrapper
public static de.donmanfred.Datastorewrapper _google = null;
                           ^
  bad class file: C:\Users\Schnattern\OneDrive\B4X Libs\Add B4J\googlecloudstorage.jar(de/donmanfred/Datastorewrapper.class)
    class file has wrong version 55.0, should be 52.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.

Amazon S3 was easier (but expensive), because there is IP whitelisting there.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
class file has wrong version 55.0, should be 52.0
i see. I am using OpenJava 11 in B4J.
But even if you would use the same Java, you would get stuck with it as it is missing some imports. I tried it yterday but did not get it working here too...

Additionally i started doing a wrap for the wrong component. You dont need the Datastore, you need the Storage. I switched to the storage but at the end i failed finding the right imports... The day ended and i stopped...
Will try to investivate but guess it needs some time.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Will try to investivate but guess it needs some time.
Thanks for your time, maybe i should look for an alternative with a better auth. mehtod than google's.

You can get the token with a UI app and then use it in the non-ui app.
as far as I know, a token is only valid for 2 weeks, then you have to update this
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
GoogleOAuth2 class takes care of it. It uses the refresh token to get a new access token. The UI part where the user approves the service is only required once.
So I only need 1 time to create a token with the UI application. Then I can pak the code in the non-Ui and he updates then by itself?
Because, later i have 20+ VPS's, It should be done automatically
 
Upvote 0
Top