-2

I call an API in Visual Basic and it returns me this:

"access_token": "21652337f6733aec846eecd28bc421642a040ed68fd288bb2e1b96a3fa3",
"token_type": "bearer",
"expires_in": 7776000,
"created_at": 1471158248

I can save this response to a variable, but how do I filter out everything but the Access Token and then save that to a variable?

Thanks.

3

1 Answer 1

1
Public Function GetAccessToken(input As String) As String
    Dim result = System.Text.RegularExpressions.Regex.Match(input, """access_token"":\s*""([^""]*)""")
    If result.Success Then
        Return result.Groups(1).Value
    End If
    Return ""
End Function
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks that worked but its not complete. The output I get from your code gives me the whole access_token line. I just want the actual random string minus quotations. So my output would look like: 21652337f6733aec846eecd28bc421642a040ed68fd288bb2e1b96a3fa3 ...No more no less. Thanks for the help.
Odd, are you sure you copied it exactly? There's only 1 capture group defined so groups(0) would give the whole line, groups(1) should give your value.
Well, no. I didn't quite copy it exactly as your Public Function would not work inside of my sub, so i had to Frankenstein it with my very limited knowledge of VB. The line I did use was this one: Dim result = System.Text.RegularExpressions.Regex.Match(input, """access_token"":\s*""([^""]*)""") and when I printed the output of result.value I get the whole access token line. I did manage to work around this in the most convoluted method possible, but it does work. If you could make this one line only include the access token minus quotations I would be very happy.
Instead of printing result.Value print result.Groups(1).Value. However trying to fit things onto one line is generally a bad practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.