B4J Question Google Cloud Storage authentication

Discussion in 'B4J Questions' started by Alexander Stolte, Feb 16, 2019.

  1. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed 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
     
  2. DonManfred

    DonManfred Expert Licensed User

    The OAuth2 Class does work with B4J too
     
  3. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    for non-ui apps too?
     
  4. DonManfred

    DonManfred Expert Licensed 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.
     
    Alexander Stolte likes this.
  5. DonManfred

    DonManfred Expert Licensed 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.
     

    Attached Files:

    Alexander Stolte likes this.
  6. DonManfred

    DonManfred Expert Licensed User

    Code:
    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 ;-)
     
  7. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    The compiler said:
    Code:
    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.
     
  8. DonManfred

    DonManfred Expert Licensed User

    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.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    You can get the token with a UI app and then use it in the non-ui app.
     
  10. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Thanks for your time, maybe i should look for an alternative with a better auth. mehtod than google's.

    as far as I know, a token is only valid for 2 weeks, then you have to update this
     
  11. Erel

    Erel Administrator Staff Member Licensed 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.

    I have such a service running for months, downloading data from Google Sheet every few minutes.
     
    DonManfred likes this.
  12. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    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
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Yes. You just need to copy the oauth2token.dat file from the UI app data folder to the non-ui app data folder.
     
    Alexander Stolte likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice