3
AccessToken with Expiration Date previous to current
Problem reported by Bruno Gomes - 7/31/2020 at 10:25 AM
Submitted
Good afternoon, 

I am trying to implement a solution to reconstruct the users' folders using the API. I can authenticate and get AccessToken but when I try to run the RestoreFolders method I get the error: 401 Not Authorized. I noticed that the token's Expiration Date is exactly 45 minutes before the current time. I tried to do the RefreshToken but I get the same result. Is this the cause of the problem? What am I doing wrong? I need urgent help, please. Follow my code below:

Dim requestAuthentication As New RequestAuthentication()

        requestAuthentication.username = "admin"
        requestAuthentication.password = "admin123*"

        client.Headers.Add(HttpRequestHeader.ContentType, "application/json")

        Dim jsonStr As String = client.UploadString("http://localhost:9998/api/v1/auth/authenticate-user";, JsonConvert.SerializeObject(requestAuthentication))

        Dim responseAuthentication As ResponseAuthentication = JsonConvert.DeserializeObject(Of ResponseAuthentication)(jsonStr)
        accessToken = responseAuthentication.refreshToken

        Dim requestRefreshToken As New RequestRefreshToken()

        requestRefreshToken.token = responseAuthentication.refreshToken

        client.Headers.Add(HttpRequestHeader.ContentType, "application/json")
        jsonStr = client.UploadString("http://localhost:9998/api/v1/auth/refresh-token";, JsonConvert.SerializeObject(requestRefreshToken))

        Dim responseRefreshToken As ResponseRefreshToken = JsonConvert.DeserializeObject(Of ResponseRefreshToken)(jsonStr)
        accessToken = responseRefreshToken.accessToken

        client.Headers.Add("Authorization", accessToken)

        Dim requestRestoreFolders As New RequestRestoreFolders()

        requestRestoreFolders.restorations = New Restorations() With
            {
            .email = "bruno@zagloman.com",
            .folder = "Inbox",
            .recursive = False
            }

        jsonStr = client.UploadString("http://localhost:9998/api/v1/settings/sysadmin/restore-folders";, JsonConvert.SerializeObject(requestRestoreFolders))

        Dim responseRestoreFolders As ResponseRestoreFolders = JsonConvert.DeserializeObject(Of ResponseRestoreFolders)(jsonStr)
Thank's.

2 Replies

Reply to Thread
0
Bruno Gomes Replied
Someone who can help me? Does anyone have any examples in C#?
Thank's
1
Sébastien Riccio Replied
I wrote a dirty script in powershell for this some time ago... Maybe it can help you. It was:

# Settings
$SMBaseURL = 'https://smartermail.url/api/v1';
$APIUsername = 'someadminuser'
$APIPass = 'somepass'

$global:Header = ""

# Functions
function RestoreFolder($Account, $Folder, $Recursive)
{
    # Rebuild user folder
    $SMURL = "$SMbaseurl/settings/sysadmin/restore-folders"
    $params = @{
        "restorations" = @(
            @{
            "email" = $Account
            "folder" = $Folder
            "recursive" = $Recursive
            }
        )
    }
    $params = ConvertTo-Json -InputObject $params
    $response = Invoke-RestMethod -Method POST -Headers $Header -ContentType "application/json" -uri $SMURL -Body $params -ErrorAction SilentlyContinue -ErrorVariable RestError

    if ($response.success = $true -And (-not [string]::IsNullOrWhiteSpace($response.message)))
    {
        return "Error: " + $response.message
    }
    return "Ok."
}

#
# Main code
# 

#  Authenticate and get token
$SMURL = "$SMBaseURL/auth/authenticate-user"
$params = @{
    username = $APIUsername
    password = $APIPass
} | ConvertTo-Json
$response = Invoke-RestMethod -Method POST -ContentType "application/json" -uri $SMURL -Body $params -ErrorAction SilentlyContinue -ErrorVariable RestError

#Write-Host $response.accessToken
if ($response.resultCode = 200)
{
    $accesstoken = $response.accessToken
    $refreshtoken = $response.refreshToken
    $Header = @{Authorization = "Bearer " + $accesstoken}
}
else {
    Write-Host "Error: Could not authenticate on ${SMURL}"
    exit
}

# Now lets give it a go...
RestoreFolder "someuser@domain.xxx" "Inbox" $true


Sébastien Riccio System & Network Admin https://swisscenter.com

Reply to Thread