Initialization and login
suggest change- Create a new application here: create application
- Choose standalone application and confirm app creation via SMS.
- 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.
- 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
- 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 ifgetCertificateFingerprintreturns an empty array. - Then add this to your gradle file:
compile 'com.vk:androidsdk:1.6.5'
- Initialize the SDK on startup using the following method. The best way is to call it in the
ApplicationonCreatemethod. Replace1234567with 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.
- Final step is to login using vksdk.
someButtonForLoginis the button from your layout. Inside the click listener,thisis the listener, sologinneeds 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();
}
});
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents