.netprotocol-buffersandroid-management-api

How to generate Protobuf Duration for Enrollment Token?


I am trying to create an enrollment token that expires at the end of the day. However, when I try to send a Create Request, I get the following message:

service androidmanagement has thrown an exception.
HttpStatusCode is BadRequest.
Google.Apis.Requests.RequestError
Invalid JSON payload received. Unknown name "Seconds" at 'enrollment_token.duration': Cannot find field.
Invalid JSON payload received. Unknown name "Nanos" at 'enrollment_token.duration': Cannot find field. [400]
Errors [
        Message[Invalid JSON payload received. Unknown name "Seconds" at 'enrollment_token.duration': Cannot find field.
Invalid JSON payload received. Unknown name "Nanos" at 'enrollment_token.duration': Cannot find field.] Location[ - ] Reason[invalid] Domain[]
]

When I debug the token I receive from my original request, the Duration field is (null). There is an ExperationTimestamp of 1 hr since creation.

The website says we can customize the duration: https://developers.google.com/android/management/provision-device#create_an_enrollment_token

But looking at the documentation for creating new Enrollment Tokens, there is no field to customize duration: https://developers.google.com/android/management/reference/rest/v1/enterprises.enrollmentTokens/create

Is this a deprecated feature?

The complete code I am using is here:

    // Calculate Duration
    Timestamp now = Timestamp.FromDateTime(DateTime.UtcNow);
    Timestamp tomorrow = Timestamp.FromDateTime(DateTime.Today.AddDays(1).ToUniversalTime());

    // Create a token with a policy name. Whatever device provisioned with this token will use this policy
    EnrollmentToken token = new EnrollmentToken();
    token.PolicyName = policyID;
    token.Duration = tomorrow - now;

    // Build and execute a request to tie the token to our enterprise
    EnterprisesResource.EnrollmentTokensResource.CreateRequest request = androidManagementService.Enterprises.EnrollmentTokens.Create(token, enterprise.Name);
    var result = request.Execute();

Solution

  • The package says token.Duration is an object. I assumed it wanted a Duration class.

    However, the api only accepts a string of format {timeInSeconds}s

    This is the working piece of code:

        // Calculate Duration
        var expirationTimeSpan = DateTime.Today.AddDays(1) - DateTime.Now;
        string expireDurationString = expirationTimeSpan.TotalSeconds.ToString() + "s";
    
        // Create a token with a policy name. Whatever device provisioned with this token will use this policy
        EnrollmentToken token = new EnrollmentToken();
        token.PolicyName = policyID;
        token.Duration = expireDurationString;
    
        // Build and execute a request to tie the token to our enterprise
        EnterprisesResource.EnrollmentTokensResource.CreateRequest request = androidManagementService.Enterprises.EnrollmentTokens.Create(token, enterprise.Name);
        var result = request.Execute();