Easy social network authorization for Android. Supports Facebook, Twitter, Instagram, Google+, Vkontakte. Made by Stfalcon
SocialAuthHelper
A library that helps to implement social network authorization (Facebook, Twitter, Instagram, GooglePlus, Vkontakte).Who we are
Need iOS and Android apps, MVP development or prototyping? Contact us via info@stfalcon.com. We develop software since 2009, and we're known experts in this field. Check out our portfolio and see more libraries from stfalcon-studio.Download
Download via Gradle:
compile 'com.github.stfalcon:socialauthhelper:0.1'
or Maven:
<dependency> <groupId>com.github.stfalcon</groupId> <artifactId>socialauthhelper</artifactId> <version>0.1.1</version> <type>pom</type> </dependency>
Usage
If you don't use fabric plugin for Android studio, put it into gradle:
repositories { maven { url 'https://maven.fabric.io/public' } }
buildscript { repositories { mavenCentral() maven { url 'https://maven.fabric.io/public' } }
dependencies { classpath 'io.fabric.tools:gradle:1.+' } }
dependencies { //your dependencies compile ('com.twitter.sdk.android:twitter:1.13.2@aar') { transitive = true; } }
Don't forget to enable: "Allow this application to be used to Sign in with Twitter."
In Application class or initial activity class:
TwitterAuthConfig authConfig = new TwitterAuthConfig( getString(R.string.twitterConsumerKey),//twitter application consumer key getString(R.string.twitterConsumerSecret));//twitter application consumer secret //setup fabric Fabric.with(this, new Twitter(authConfig)); In your activity(fragment) class declare field: private TwitterClient twitterClient; In onCreate method: //create TwitterClient where this is your activity twitterClient = new TwitterClient(this);
//init views final Button btnTwitter = (Button) findViewById(R.id.btn_twitter); final TextView tvTwitter = (TextView) findViewById(R.id.tv_twitter); final ImageView ivTwitter = (ImageView) findViewById(R.id.iv_twitter);
//set onClick event for auth button btnTwitter.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { twitterClient.getUser(new TwitterClient.UserLoadListener() { @Override public void onUserLoaded(User user, String profileImage) { //after authorization successful you have access to user profile and Access Token tvTwitter.setText(getString(R.string.profileInfo, user.name, user.getId(), twitterClient.getAccessToken()));
Picasso.with(MainActivity.this).load(profileImage).into(ivTwitter); } });
Override onActivityResult method: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { twitterClient.onActivityResult(requestCode, resultCode, data); } Vkontakte
Create new application at https://vk.com/devIn your activity(fragment) class declare field:
private VkClient vkClient; In onCreate method: //create VkClient where this is your activity vkClient = new VkClient(this, //activity or fragment getString(R.string.vkredirecturi), //vk application redirect uri getString(R.string.vkclientid)); //vk application clientId
//init views final Button btnVk = (Button) findViewById(R.id.btn_vk); final TextView tvVk = (TextView) findViewById(R.id.tv_vk); final ImageView ivVk = (ImageView) findViewById(R.id.iv_vk); //set onClick event for auth button btnVk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vkClient.getProfile(new VkClient.DataLoadedListener<VkProfile>() { @Override public void onDataLoaded(VkProfile vkProfile) { //after authorization successful you have access to user profile and Access Token tvVk.setText(getString(R.string.profileInfo, vkProfile.getFirstName() + " " + vkProfile.getLastName(), vkProfile.getId(), vkClient.getAccessToken())); Picasso.with(MainActivity.this).load(vkProfile.getProfilePhoto()).into(ivVk); } }); } });
Override onActivityResult method: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { vkClient.onActivityResult(requestCode, resultCode, data); } In AndroidManifest:
<activity android:name="com.facebook.FacebookActivity" android:c android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <provider android:name="com.facebook.FacebookContentProvider" android:authorities="com.facebook.app.FacebookContentProvider<Your facebook application ID>" android:exported="true" /> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="<Your facebook application ID>" />
In your activity(fragment) class declare field:
private FacebookClient facebookClient; In onCreate method: facebookClient = new FacebookClient(this);
//init views final Button btnFacebook = (Button) findViewById(R.id.btn_facebook); final TextView tvFacebook = (TextView) findViewById(R.id.tv_facebook); final ImageView ivFacebook = (ImageView) findViewById(R.id.iv_facebook);
//set onClick event for auth button btnFacebook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { facebookClient.getProfile(new FacebookClient.FbResultCallback() { @Override public void onProfileLoaded(FacebookProfile facebookProfile) { //after authorization successful you have access to user profile and Access Token tvFacebook.setText(getString(R.string.profileInfo, facebookProfile.getName(), facebookProfile.getId(), facebookClient.getToken())); Picasso.with(MainActivity.this).load( facebookProfile.getPicture().data.url).into(ivFacebook); } }); } });
Override onActivityResult method: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { facebookClient.onActivityResult(requestCode, resultCode, data); }
Google+
Create new application at https://console.developers.google.com/Don't forget to enable google plus api.
In your activity(fragment) class declare field:
private GooglePlusClient googlePlusClient;
In onCreate method:
googlePlusClient = new GooglePlusClient(this,
getString(R.string.googleClientId));//Web client id
//init views final Button btnGoogle = (Button) findViewById(R.id.btn_google); final TextView tvGoogle = (TextView) findViewById(R.id.tv_google); final ImageView ivGoogle = (ImageView) findViewById(R.id.iv_google); //set onClick event for auth button btnGoogle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { googlePlusClient.getProfile(new GooglePlusClient.GooglePlusResultCallback() { @Override public void onProfileLoaded(GooglePlusProfile googlePlusProfile) { //after authorization successful you have access to user profile and Access Token tvGoogle.setText(getString(R.string.profileInfo, googlePlusProfile.getName(), googlePlusProfile.getId(), facebookClient.getToken())); Picasso.with(MainActivity.this).load( googlePlusProfile.getAvatar()).into(ivGoogle); } }); } });
Override onActivityResult method: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { googlePlusClient.onActivityResult(requestCode, resultCode, data); }
Don't forget to enable implicit OAuth in application security settings.
In your activity(fragment) class declare field:
private InstagramClient instagramClient; In onCreate method: instagramClient = new InstagramClient(this, getString(R.string.instagramRedirectUri), //instagram application redirect uri getString(R.string.instagramClientId)); //instagram application client id
//init views final Button btnInstagram = (Button) findViewById(R.id.btn_instagram); final TextView tvInstagram = (TextView) findViewById(R.id.tv_instagram); final ImageView ivInstagram = (ImageView) findViewById(R.id.iv_instagram);
//set onClick event for auth button btnInstagram.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { instagramClient.getProfile(new InstagramClient.DataLoadedListener<InstagramProfile>() { @Override public void onDataLoaded(InstagramProfile instagramProfile) { //after authorization successful you have access to user profile and Access Token tvInstagram.setText(getString(R.string.profileInfo, instagramProfile.getFullName(), instagramProfile.getId(), facebookClient.getToken())); Picasso.with(MainActivity.this).load( instagramProfile.getProfilePicture()).into(ivInstagram); } }); } });
Override onActivityResult method:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { instagramClient.onActivityResult(requestCode, resultCode, data); }
Take a look at the sample projects for more information
License
Copyright 2017 stfalcon.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
[sample]: