Search Unity

Question How to add access token to CCD request to private bucket

Discussion in 'Unity Cloud Content Delivery' started by Limewood_, Nov 16, 2022.

  1. Limewood_

    Limewood_

    Joined:
    Jan 5, 2020
    Posts:
    62
    Note: I posted this in the Addressables forum first, but this seems a more appropriate place for it.

    I've set up buckets on CCD and created an Addressables profile using the 'latest' badge in the project.
    I've set the buckets private and created an access token for each of my buckets.
    Finally, I've used the code from here to try to add the base64 encoded token to the request:
    https://docs.unity.com/ccd/UnityCCDWalkthrough.html?TocPath=Unity CCD + Addressables walkthrough|_____0#The_Command-line_interface_(optional)
    Code (CSharp):
    1. Addressables.WebRequestOverride = webRequest =>
    2. {
    3. webRequest.SetRequestHeader("Authorization", base64encodedToken);
    4. };
    5. var downloadHandle = Addressables.DownloadDependenciesAsync(downloadTags, Addressables.MergeMode.Union, true);
    I'm getting a 401 Unauthorized response each time.
    I've tried with both tokens, base64 encoded and not, adding "Bearer: " in front of the token and not.
    I've also tried adding "Basic " in front of the token, as recommended by this thread (which got a worryingly late reply): https://forum.unity.com/threads/dont-know-use-access-tokens-private-bucket.1294164/
    I've set the addressables groups to "Use UnityWebRequest for" as well.
    How is it supposed to be done?

    Also, when we find out how it's actually supposed to work, could you please update the example at the bottom here?
    https://docs.unity.com/ccd/UnityCCDWalkthrough.html?TocPath=Unity CCD + Addressables walkthrough|_____0#The_Command-line_interface_(optional)
     
  2. Limewood_

    Limewood_

    Joined:
    Jan 5, 2020
    Posts:
    62
    Also, what is the use for public buckets where anyone can upload and access content without authentication (e.g. "If a bucket is tagged “Open to all”, anyone can modify content in this bucket" from https://blog.unity.com/games/whats-new-in-cloud-content-delivery)? Is it really a bucket where anyone in the world can upload content if they know the URL?
     
  3. Limewood_

    Limewood_

    Joined:
    Jan 5, 2020
    Posts:
    62
    @kaanyalti Did you find out how it's supposed to work with private buckets and access tokens?
     
  4. Limewood_

    Limewood_

    Joined:
    Jan 5, 2020
    Posts:
    62
    Well, I found out the solution, but I feel like this is either a bug or something is missing in all documentation about this.
    I noticed that on the following page there is an example of an access token and its Base64 encoded counterpart, but they don't match: https://content-api.cloud.unity3d.com/doc_client/
    However, decoding the Base64 version gives the example token with ":" prepended, so I tried adding a colon in front of my access token before encoding to Base64 and then it started working.
    Is this a mistake or is it intended to have a colon in front to work? And if so, why is this not documented anywhere?
     
  5. llunity3d

    llunity3d

    Unity Technologies

    Joined:
    Aug 23, 2021
    Posts:
    24
    Hi @Limewood_ ,

    Yes that is intended, in the documentation https://content-api.cloud.unity3d.com/doc_client/, just a few line above the base64 encoded counterpart, it is mentioned that :

    To authenticate requests, include a Basic Authentication header as a base64-encoded string 'username:password', using your access token as the password (and empty username).


    So you get something like have ':access token' and encode it in base64 afterward.

    We will update it to make it clearer,

    Hope this helps.
     
  6. Limewood_

    Limewood_

    Joined:
    Jan 5, 2020
    Posts:
    62
    Oh, I see now then, thank you for clarifying.
     
  7. kenamis

    kenamis

    Joined:
    Feb 5, 2015
    Posts:
    387
    I am wondering about this too.
    I assume only the Unity accounts tied to the project can write to open buckets? According to the Management API
    then later under permissions
    It's very unclear.

    I'm using Addressables with the CCD and the CCD management package to build and release directly from the editor. I have a Staging environment and bucket with open, so I can write to it, and a production bucket with "promotion only", to promote to if the staging bucket is correct.

    How do I make sure my staging bucket is read only for anyone except my developers?

    I know there's the newer private read access token. I don't care about people reading the staging bucket, only write access. Do I need to use the private read access to fully protect the staging bucket?

    Do I need to get the CLI to properly add permissions to my buckets that aren't available on the dashboard?
     
    enosh15 and Frozen_Monkey like this.
  8. LenoM

    LenoM

    Joined:
    Sep 19, 2018
    Posts:
    2
    Hi I want to provide some code here, which worked for me to access private buckets with a token setup. I found the Documentations example code

    1.https://docs.unity.com/ccd/en/manual/UnityCCDWalkthrough#Using_Addressables_with_private_buckets

    2.https://docs.unity3d.com/Packages/c...leAssets.Addressables.WebRequestOverride.html

    a bit contradictory.

    Please find the code below:

    Code (CSharp):
    1.         void Start()
    2.         {
    3.             //Add private token to addressable web request header
    4.             Addressables.WebRequestOverride = AddPrivateToken;
    5.         }
    6.          
    7.  
    8.         private void AddPrivateToken(UnityWebRequest request)
    9.         {
    10.             string bucketAccessToken = ""; // Add your bucket token here
    11.  
    12.             var encodedToken = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($":{bucketAccessToken}"));
    13.  
    14.             request.SetRequestHeader("Authorization", $"Basic {encodedToken}");
    15.         }
     
    Last edited: Mar 8, 2023
  9. DrSage

    DrSage

    Joined:
    Aug 1, 2018
    Posts:
    1
    It works for me in editor and returns 401 in WebGl build. Have any idea?