M
M
max_vr2016-06-17 20:37:36
Android
max_vr, 2016-06-17 20:37:36

Can't turn url into VKAccessToken?

I am writing a messenger for VK using the standard vk sdk. There is this piece of code:

String url = "https://oauth.vk.com/blank.html#access_token=https://oauth.vk.com/blank.html#access_token=af97823a79fe195e120c9d0c0190ce9b373e7d93fb3f2b2abc9066e361fbcaecdd9e761d****f0e18095&expires_in=0&user_id=****&email=****@gmail.com"
        VKAccessToken t = VKAccessToken.tokenFromUrlString(url);
        t.save();

(Personal data closed with asterisks)
In it, you need to get an accessToken from the url, and then save it. The problem is that t remains null for unknown reasons, causing the program to crash on the save() method. Just in case, I post several methods from the VK SDK:
public static VKAccessToken tokenFromUrlString(String urlString) {
        if (urlString == null)
            return null;
        Map<String, String> parameters = VKUtil.explodeQueryString(urlString);
        return tokenFromParameters(parameters);

 public static Map<String, String> explodeQueryString(@Nullable String queryString) {
        if (queryString == null) {
            return null;
        }
        String[] keyValuePairs = queryString.split("&");
        HashMap<String, String> parameters = new HashMap<>(keyValuePairs.length);

        for (String keyValueString : keyValuePairs) {
            String[] keyValueArray = keyValueString.split("=");
            parameters.put(keyValueArray[0], keyValueArray[1]);
        }
        return parameters;
    }

 public static VKAccessToken tokenFromParameters(@Nullable Map<String, String> parameters) {
        if (parameters == null || parameters.size() == 0) {
            return null;
        }
        VKAccessToken token = new VKAccessToken();
        try {
            token.accessToken = parameters.get(ACCESS_TOKEN);
            token.userId = parameters.get(USER_ID);
            token.secret = parameters.get(SECRET);
            token.email = parameters.get(EMAIL);
            token.httpsRequired = false;
            if (parameters.get(EXPIRES_IN) != null) { token.expiresIn = Integer.parseInt(parameters.get(EXPIRES_IN)); }

            String scope = parameters.get(SCOPE);
            if (scope != null) {
                HashMap<String, Boolean> scopeMap = new HashMap<>();
                for (String s : scope.split(",")) {
                    scopeMap.put(s, true);
                }
                token.scope = scopeMap;
            }

            if (parameters.containsKey(HTTPS_REQUIRED)) {
                token.httpsRequired = parameters.get(HTTPS_REQUIRED).equals("1");
            } else if (token.secret == null) {
                token.httpsRequired = true;
            }

            if (parameters.containsKey(CREATED)) {
                token.created = Long.parseLong(parameters.get(CREATED));
            } else {
                token.created = System.currentTimeMillis();
            }

            return token.accessToken != null ? token : null;
        } catch (Exception e) {
            return null;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-06-17
@Rou1997

Your URL is not correct at all, I don’t know where you get it from, it will work like this:

https://oauth.vk.com/blank.html#access_token=af97823a79fe195e120c9d0c0190ce9b373e7d93fb3f2b2abc9066e361fbcaecdd9e761d****f0e18095&expires_in=0&user_id=****&email=****@gmail.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question