VKSdk.authorize()之后的VKRequest抛出错误“无效的用户ID”。一个接一个?

时间:2015-04-29 10:19:04

标签: android api authentication login vk

点击Button,我要求获取用户个人资料信息。我调试了,当我刚刚打开应用程序并点击按钮onError时,回调工作,它说“无效的用户ID”。如果我第二次按Button它可以正常工作。从实验中我了解到应用程序没有完成授权,这就是请求被拒绝的原因。 使用此代码按Button调用方法:

VKSdk.authorize(VKScopes.EMAIL);
VKRequest request = VKApi.users().get(VKParameters.from(VKApiConst.FIELDS,
    "user_id,first_name,last_name,sex,bdate,city,photo_200_orig"));
request.secure = false;
request.useSystemLanguage = true;
request.executeWithListener(new VKRequest.VKRequestListener() {
     @Override
     public void onComplete(VKResponse response) {
         super.onComplete(response);
         //do some stuff
     }
     @Override
     public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
          super.attemptFailed(request, attemptNumber, totalAttempts);
          Log.d("VkDemoApp", "attemptFailed " + request + " " + attemptNumber + " " + totalAttempts);
    }
    @Override
    public void onError(VKError error) {
         super.onError(error);
         Log.d("VkDemoApp", "onError: " + error);
    }
    @Override
    public void onProgress(VKRequest.VKProgressType progressType, long bytesLoaded, long bytesTotal) {
         super.onProgress(progressType, bytesLoaded, bytesTotal);
         Log.d("VkDemoApp", "onProgress " + progressType + " " + bytesLoaded + " " + bytesTotal);
    }
});

我尝试添加if(VKSdk.wakeUpSession()),但它只是通过,因为用户尚未获得授权。我在示例回调onTokenExpired中找到了listener,我会在需要时授权用户,但是当我添加它时说“从未使用过”。

我怎样才能等到VKSdk.authorize()完成工作后才能发送请求? 提前谢谢!

修改

这是onCreate()

中的一行
VKSdk.initialize(sdkListener, activity.getResources().getString(R.string.vk_app_id));
VKUIHelper.onCreate(this);

VKSdkListener

VKSdkListener sdkListener = new VKSdkListener() {
    @Override
    public void onAcceptUserToken(VKAccessToken token) {
    }

    @Override
    public void onReceiveNewToken(VKAccessToken newToken) {
        //requesting the code at the top (request)
        //excluding the line VKSdk.authorize(VKScopes.EMAIL);
    }

    @Override
    public void onRenewAccessToken(VKAccessToken token) {
    }
    @Override
    public void onCaptchaError(VKError vkError) {
    }
    @Override
    public void onTokenExpired(VKAccessToken vkAccessToken) {
    }
    @Override
    public void onAccessDenied(VKError vkError) {
    }
};

并在onClick()中有一行VKSdk.authorize(VKScopes.EMAIL);

注意 VkSdkListener的回调未在initialize()之后调用,而是在authorize()之后调用。我不知道为什么,这种行为很可怕。很高兴看到上面列出的用户电子邮件和其他内容的工作示例。

1 个答案:

答案 0 :(得分:2)

我可能错了,但试着改变" user_id"到" id"。 第二个问题(如何在授权后获取数据)的答案很简单。 VKsdk有一个可以(并且应该)覆盖的抽象方法的监听器。

修改

private static final String[] sVkontakteScopes = new String[]{
        VKScope.FRIENDS,
        VKScope.PHOTOS,
        VKScope.NOHTTPS,
};

private VKSdkListener mVkontakteLoginListener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, persistentState);

    mVkontakteLoginListener = new VKSdkListener() {
        @Override
        public void onCaptchaError(VKError captchaError) {
            new VKCaptchaDialog(captchaError).show(VkApiActivity.this);
        }

        @Override
        public void onTokenExpired(VKAccessToken expiredToken) {
            VKSdk.authorize(sVkontakteScopes);
        }

        @Override
        public void onAccessDenied(VKError authorizationError) {
            new AlertDialog.Builder(VKUIHelper.getTopActivity())
                    .setMessage(authorizationError.toString())
                    .show();
        }

        @Override
        public void onReceiveNewToken(VKAccessToken newToken) {
            //put your method here
            makeYourStuff();
            PrefUtils.storeVkontakteAccessToken(newToken.accessToken);
        }

        @Override
        public void onAcceptUserToken(VKAccessToken token) {
            //put your method here
            makeYourStuff();
        }

        @Override
        public void onRenewAccessToken(VKAccessToken token) {
            //put your method here
            makeYourStuff();
        }
    };

    if (PrefUtils.getVkontakteAccessToken() != null)
        VKSdk.initialize(mVkontakteLoginListener, getString(R.string.vkontakte_app_id, PrefUtils.getVkontakteAccessToken()));
    else {
        VKSdk.initialize(mVkontakteLoginListener, getString(R.string.vkontakte_app_id));
    }
    VKUIHelper.onCreate(this);
}


private void makeYourStuff() {
    VKParameters params = new VKParameters();
    params.put(VKApiConst.FIELDS, "id,first_name,last_name, photo_100");

    VKRequest request = new VKRequest("users.get", params);
    request.executeWithListener(new VKRequest.VKRequestListener() {

        @Override
        public void onComplete(VKResponse response) {
            super.onComplete(response);
            JSONArray jsonArray;
            String firstName, lastName, id, photoUrl;


            try {
                jsonArray = response.json.getJSONArray("response");
                JSONObject userObject = jsonArray.getJSONObject(0);
                firstName = userObject.getString("first_name");
                lastName = userObject.getString("last_name");
                id = userObject.getString("id");
                photoUrl = userObject.getString("photo_100");

                /*
                    here you can start another activity or fragment

                    put this info into bundle or store into database

                */
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(VKError error) {
            super.onError(error);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    VKUIHelper.onActivityResult(this, requestCode, resultCode, data);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnLogin) {
        VKSdk.authorize(sVkontakteScopes);
        // after this method will be called your one of your          VKSdkListener methods
        // you should put your request method all three SUCCESS methods of VKSdk listener
    }
}