Initialization and login

suggest change
  1. Create a new application here: create application
  2. Choose standalone application and confirm app creation via SMS.
  3. Fill Package name for Android as your current package name. You can get your package name inside the Android manifest file, at the very beginning.
  4. Get your Certificate fingerprint by executing this command in your shell/cmd:
keytool -exportcert -alias androiddebugkey -keystore path-to-debug-or-production-keystore -list -v

You can also get this fingerprint by SDK itself:

String[] fingerprints = VKUtil.getCertificateFingerprint(this, this.getPackageName());
Log.d("MainActivity", fingerprints[0]); // crashes when the array is empty
  1. Add the received fingerprint into your Signing certificate fingerprint for Android: field in Vk app settings (where you entered your package name). fingerprints[0] throws if getCertificateFingerprint returns an empty array.
  2. Then add this to your gradle file:
compile 'com.vk:androidsdk:1.6.5'
  1. Initialize the SDK on startup using the following method. The best way is to call it in the Application onCreate method. Replace 1234567 with the numeric app id from the VK settings.
private static final int VK_ID = 1234567;
public static final String VK_API_VERSION = "5.52"; //current version
@Override
    public void onCreate() {
        super.onCreate();
        VKSdk.customInitialize(this, VK_ID, VK_API_VERSION);
}

This is the best way to initialize VKSdk. Don’t use the method where VK_ID should be placed inside strings.xml because the API will not work correctly after it.

  1. Final step is to login using vksdk. someButtonForLogin is the button from your layout. Inside the click listener, this is the listener, so login needs the Activity.
public static final String[] VK_SCOPES = new String[]{
        VKScope.FRIENDS,
        VKScope.MESSAGES,
        VKScope.NOTIFICATIONS,
        VKScope.OFFLINE,
        VKScope.STATUS,
        VKScope.STATS,
        VKScope.PHOTOS
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    someButtonForLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           VKSdk.login(SocialNetworkChooseActivity.this, VK_SCOPES); 
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    VKSdk.onActivityResult(requestCode, resultCode, data, new VKCallback<VKAccessToken>() {
        @Override
        public void onResult(VKAccessToken res) {
            String token = res.accessToken; //getting our token here.
        }

        @Override
        public void onError(VKError error) {
            Toast.makeText(SocialNetworkChooseActivity.this,
                    "User didn't pass Authorization", Toast.LENGTH_SHORT).show();
        }
    });
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents