so here is my code for connecting and getting the values:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook;
using SmartLocalization;
public class mainMenuFacebook : MonoBehaviour {
public string FBname;
public string FBsurname;
Dictionary<string, string> profile = new Dictionary<string, string>();
// Use this for initialization
void OnMouseDown()
{
FB.Login("publish_actions,public_profile", LoginCallback); // logine tıklama
}
void Awake() {
FB.Init(SetInit, OnHideUnity); //facebook başlangıcı
}
private void SetInit()
{
if (FB.IsLoggedIn)
{
// Util.Log("Already logged in");
OnLoggedIn();
}
}
private void OnHideUnity(bool isGameShown)
{
if (!isGameShown)
{
// pause the game - we will need to hide
Time.timeScale = 0;
}
else
{
// start the game back up - we're getting focus again
Time.timeScale = 1;
}
}
void LoginCallback(FBResult result)
{
Util.Log("LoginCallback");
if (FB.IsLoggedIn)
{ gameObject.guiTexture.enabled = false;
OnLoggedIn();
}
}
void OnLoggedIn()
{
FB.API("/me?fields=first_name,last_name,email", Facebook.HttpMethod.GET, APICallback); // adını ve idyi çekiyoruz.
}
void APICallback(FBResult result)
{
if (result.Error != null)
{
// Let's just try again
// FB.API("/me?fields=id,first_name,last_name,email,friends.limit(100).fields(first_name,last_name,id)", Facebook.HttpMethod.GET, APICallback);
return;
}
Debug.Log(result.Text);
profile = Util.DeserializeJSONProfile(result.Text);
FBname = profile["first_name"];
FBsurname = profile["last_name"]; // **IT GIVES ERROR**
Debug.Log(FBsurname + " " + FBname);
//PlayerPrefs.SetString("surname",profile["last_name"]);
//PlayerPrefs.SetString("email",profile["email"]);
gameObject.guiTexture.enabled = false;
GameObject.Find("Wellcome").guiText.enabled = true;
GameObject.Find("Wellcome").guiText.text = LanguageManager.Instance.GetTextValue("menu.hosgeldin") + " <b><color=#ffa500ff>" + FBname + "</color></b>, <i>" + LanguageManager.Instance.GetTextValue("menu.cikis") +"</i>";
PlayerPrefs.SetString("name",FBname);
}
}
when i only try to get first_name everything is okay. But i need to get last_name and email too. I think i cant serialize because when i try to Debug.Log(profile.Count); it shows 1.
How can i fix it?
Given error is:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,System.String].get_Item (System.String key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
mainMenuFacebook.APICallback (.FBResult result) (at Assets/Scripts/mainMenuFacebook.cs:84)
Facebook.AsyncRequestString+<Start>c__Iterator0.MoveNext ()
Try this:
public void OnMouseDown()
{
List<string> permissions = new List<string>() { "public_profile", "email" };
FB.LogInWithReadPermissions(permissions,AuthCallback);
Debug.Log("Facebook Login");
}
In AuthCallback: if user logs in successfully, get info from FB api.
private void AuthCallback(ILoginResult result)
{
if(FB.IsLoggedIn)
{
GetInfo();
}
else
{
Debug.Log("User cancelled login");
}
}
FB API returns json result, so you will need FacebookUser class to deserialize it.
class FacebookUser
{
public string id;
public string first_name;
public string last_name;
public string email;
}
public void GetInfo()
{
FB.API("/me?fields=id,first_name,last_name,email", HttpMethod.GET, result =>
{
if(result.Error != null)
{
Debug.Log("Result error");
}
var facebookUser = Newtonsoft.Json.JsonConvert.DeserializeObject<FacebookUser>(result.RawResult);
Debug.Log(" facebook id - " + facebookUser.id);
Debug.Log(" facebook first name - " + facebookUser.first_name);
Debug.Log(" facebook last name - " + facebookUser.last_name);
Debug.Log(" facebook email - " + facebookUser.email);
});
}
NOTE: You should have Email permission from facebook to access it.
Check it in Graph API Explorer
Related
public void signInWithLinkedIn(View view) {
//First check if user is already authenticated or not and session is valid or not
if (!LISessionManager.getInstance(this).getSession().isValid()) {
//if not valid then start authentication
LISessionManager.getInstance(getApplicationContext()).init(LinkedInActivity.this, buildScope()//pass the build scope here
, new AuthListener() {
#Override
public void onAuthSuccess() {
// Authentication was successful. You can now do
// other calls with the SDK.
Toast.makeText(LinkedInActivity.this, "Successfully authenticated with LinkedIn.", Toast.LENGTH_SHORT).show();
//on successful authentication fetch basic profile data of user
//LISessionManager.getInstance(getApplicationContext()).clearSession();
fetchBasicProfileData();
}
#Override
public void onAuthError(LIAuthError error) {
// Handle authentication errors
//LISessionManager.getInstance(getApplicationContext()).clearSession();
Log.e("AUTH ERROR", "Auth Error :" + error.toString());
Toast.makeText(LinkedInActivity.this, "Failed to authenticate with LinkedIn. Please try again.", Toast.LENGTH_SHORT).show();
}
}, true);//if TRUE then it will show dialog if
// any device has no LinkedIn app installed to download app else won't show anything
} else {
//LISessionManager.getInstance(getApplicationContext()).clearSession();
Toast.makeText(this, "You have already been authenticated.", Toast.LENGTH_SHORT).show();
//if user is already authenticated fetch basic profile data for user
fetchBasicProfileData();
}
}
private static Scope buildScope() {
//Check Scopes in Application Settings before passing here else you won't able to read that data
// Scope.R_CONTACTINFO
return Scope.build(Scope.R_BASICPROFILE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data);
Log.d("Access token->", LISessionManager.getInstance(getApplicationContext()).getSession().getAccessToken().getValue());
}
/**
* method to fetch basic profile data
*/
private void fetchBasicProfileData() {
//In URL pass whatever data from user you want for more values check below link
//LINK : https://developer.linkedin.com/docs/fields/basic-profile
String url = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,public-profile-url,picture-url,email-address,picture-urls::(original))";
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.getRequest(this, url, new ApiListener() {
#Override
public void onApiSuccess(ApiResponse apiResponse) {
// Success!
JSONObject responseObject = apiResponse.getResponseDataAsJson();
try {
profileURL = responseObject.getString("publicProfileUrl");
imgURL = responseObject.getString("pictureUrl");
fetchConnectionsData();
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onApiError(LIApiError liApiError) {
// Error making GET request!
Log.e("FETCH PROFILE ERROR", "Fetch profile Error :" + liApiError.getLocalizedMessage());
Toast.makeText(LinkedInActivity.this, "Failed. Please try again.", Toast.LENGTH_SHORT).show();
}
});
}
Linkedin returns "No value for accessTokenValue" and "access toke is not set". It was working like last month but suddenly it does not work and I could not find anything wrong with the code. After much digging on Google, I am still unable to find a solution. Or am I using the v1 api which I should not? Any help will be greatly appreciated.
This is because the token generated by Linkedin SDK can not be verified by backend. The best solution is to open a webview and use Linkedin web apis- Step 1 - Create a new Class LinkedinActivity
public class LinkedinActivity {
/****FILL THIS WITH YOUR INFORMATION*********/
//This is the public api key of our application
private static final String API_KEY = "apikey";
//This is the private api key of our application
private static final String SECRET_KEY = "secretcode";
//This is any string we want to use. This will be used for avoiding CSRF attacks. You can generate one here: http://strongpasswordgenerator.com/
private static final String STATE = "123456789";
//This is the url that LinkedIn Auth process will redirect to. We can put whatever we want that starts with http:// or https:// .
//We use a made up url that we will intercept when redirecting. Avoid Uppercases.
private static final String REDIRECT_URI = "https://example.com";
/*********************************************/
//These are constants used for build the urls
private static final String AUTHORIZATION_URL = "https://www.linkedin.com/oauth/v2/authorization";
private static final String ACCESS_TOKEN_URL = "https://www.linkedin.com/uas/oauth2/accessToken";
private static final String SECRET_KEY_PARAM = "client_secret";
private static final String RESPONSE_TYPE_PARAM = "response_type";
private static final String GRANT_TYPE_PARAM = "grant_type";
private static final String GRANT_TYPE = "authorization_code";
private static final String RESPONSE_TYPE_VALUE = "code";
private static final String CLIENT_ID_PARAM = "client_id";
private static final String STATE_PARAM = "state";
private static final String REDIRECT_URI_PARAM = "redirect_uri";
/*---------------------------------------*/
private static final String QUESTION_MARK = "?";
private static final String AMPERSAND = "&";
private static final String EQUALS = "=";
private WebView webView;
private ImageView close_icon;
private ProgressDialog pd;
//private OauthInterface oauthInterface;
String accessToken;
Context context;
Dialog dialog;
public LinkedinActivity(#NonNull Context context) {
this.context = context;
}
public void showLinkedin() {
dialog = new Dialog(context, R.style.AppTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.linkedin_activity);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
pd = ProgressDialog.show(context, "", "Loading...", true);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
LinkedinData linkedinData = (LinkedinData) context;
linkedinData.linkedCancel();
dialog.dismiss();
}
return true;
}
});
//oauthInterface = new OauthPresenter(this);
//get the webView from the layout
webView = (WebView) dialog.findViewById(R.id.activity_web_view);
close_icon = (ImageView) dialog.findViewById(R.id.close_icon);
close_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinkedinData linkedinData = (LinkedinData) context;
linkedinData.linkedCancel();
dialog.dismiss();
}
});
//Request focus for the webview
webView.requestFocus(View.FOCUS_DOWN);
//Show a progress dialog to the user
//Set a custom web view client
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//This method will be executed each time a page finished loading.
//The only we do is dismiss the progressDialog, in case we are showing any.
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String authorizationUrl) {
//This method will be called when the Auth proccess redirect to our RedirectUri.
//We will check the url looking for our RedirectUri.
if (authorizationUrl.startsWith(REDIRECT_URI)) {
Log.i("Authorize", "");
Uri uri = Uri.parse(authorizationUrl);
//We take from the url the authorizationToken and the state token. We have to check that the state token returned by the Service is the same we sent.
//If not, that means the request may be a result of CSRF and must be rejected.
String stateToken = uri.getQueryParameter(STATE_PARAM);
if (stateToken == null || !stateToken.equals(STATE)) {
Log.e("Authorize", "State token doesn't match");
return true;
}
//If the user doesn't allow authorization to our application, the authorizationToken Will be null.
String authorizationToken = uri.getQueryParameter(RESPONSE_TYPE_VALUE);
if (authorizationToken == null) {
Log.i("Authorize", "The user doesn't allow authorization.");
return true;
}
Log.i("Authorize", "Auth token received: " + authorizationToken);
//Generate URL for requesting Access Token
String accessTokenUrl = getAccessTokenUrl(authorizationToken);
//We make the request in a AsyncTask
new PostRequestAsyncTask().execute(accessTokenUrl);
} else {
//Default behaviour
Log.i("Authorize", "Redirecting to: " + authorizationUrl);
webView.loadUrl(authorizationUrl);
}
return true;
}
});
//Get the authorization Url
String authUrl = getAuthorizationUrl();
Log.i("Authorize", "Loading Auth Url: " + authUrl);
//Load the authorization URL into the webView
webView.loadUrl(authUrl);
dialog.show();
}
private static String getAccessTokenUrl(String authorizationToken) {
return ACCESS_TOKEN_URL
+ QUESTION_MARK
+ GRANT_TYPE_PARAM + EQUALS + GRANT_TYPE
+ AMPERSAND
+ RESPONSE_TYPE_VALUE + EQUALS + authorizationToken
+ AMPERSAND
+ CLIENT_ID_PARAM + EQUALS + API_KEY
+ AMPERSAND
+ REDIRECT_URI_PARAM + EQUALS + REDIRECT_URI
+ AMPERSAND
+ SECRET_KEY_PARAM + EQUALS + SECRET_KEY;
}
/**
* Method that generates the url for get the authorization token from the Service
*
* #return Url
*/
private static String getAuthorizationUrl() {
return AUTHORIZATION_URL
+ QUESTION_MARK + RESPONSE_TYPE_PARAM + EQUALS + RESPONSE_TYPE_VALUE
+ AMPERSAND + CLIENT_ID_PARAM + EQUALS + API_KEY
+ AMPERSAND + REDIRECT_URI_PARAM + EQUALS + REDIRECT_URI
+ AMPERSAND + STATE_PARAM + EQUALS + STATE
+ AMPERSAND + "scope=r_emailaddress";
}
private class PostRequestAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
pd = ProgressDialog.show(context, "", "loading", true);
}
#Override
protected String doInBackground(String... urls) {
if (urls.length > 0) {
String url = urls[0];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpost);
if (response != null) {
//If status is OK 200
if (response.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(response.getEntity());
//Convert the string result to a JSON Object
JSONObject resultJson = new JSONObject(result);
//Extract data from JSON Response
int expiresIn = resultJson.has("expires_in") ? resultJson.getInt("expires_in") : 0;
accessToken = resultJson.has("access_token") ? resultJson.getString("access_token") : null;
Log.e("Tokenm", "" + accessToken);
if (expiresIn > 0 && accessToken != null) {
Log.i("Authorize", "This is the access Token: " + accessToken + ". It will expires in " + expiresIn + " secs");
//Calculate date of expiration
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, expiresIn);
long expireDate = calendar.getTimeInMillis();
////Store both expires in and access token in shared preferences
SharedPreferences preferences = context.getSharedPreferences("user_info", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("expires", expireDate);
editor.putString("accessToken", accessToken);
//oauthInterface.oauthAuthentication(accessToken, "linkedin", new HackedPrefence(getApplicationContext()).getDevice_token());
editor.commit();
return accessToken;
}
}
}
} catch (IOException e) {
Log.e("Authorize", "Error Http response " + e.getLocalizedMessage());
} catch (ParseException e) {
Log.e("Authorize", "Error Parsing Http response " + e.getLocalizedMessage());
} catch (JSONException e) {
Log.e("Authorize", "Error Parsing Http response " + e.getLocalizedMessage());
}
}
return accessToken;
}
#Override
protected void onPostExecute(String status) {
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
LinkedinData linkedinData = (LinkedinData) context;
linkedinData.LinkedinSuccess(status);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
interface LinkedinData {
void linkedCancel();
void LinkedinSuccess(String Token);
}
}
Step 2 - Call LinkdinActivity.java class using the following code
new LinkedinActivity(this).showLinkedin();
You can call the above code on the click of a button. Example
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new LinkedinActivity(this).showLinkedin();
}
});
Here is linkedin_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/activity_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/close_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:src="#drawable/ic_close_black"
android:layout_alignParentRight="true"/>
</RelativeLayout>
I have a problem with the Facebook SDK. It works fine in Editor and in apk(if i check the development build option in Unity), but on release build it doesn't work.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using UnityEngine.UI;
using Facebook.MiniJSON;
public class FBManager : MonoBehaviour {
[SerializeField]private Text FBErrorText;
public RawImage FBProfilePicture;
public Image DefaultAvatar;
public Text UsernameText;
public Text WelcomeMessage;
public Button FacebookButton;
private Color fbProfilePictureColor;
private Color defaultAvatarColor;
private string getData;
private string FBUserName;
void Awake()
{
if (!FB.IsInitialized)
{ FBErrorText.text = "fb not ini, initializing";
FB.Init(InitCallBack, OnHideUnity);
FBErrorText.text = "called fb.init";
}
else
{ FBErrorText.text += "initialized";
FB.ActivateApp();
FBErrorText.text += "called fb.activateApp";
}
fbProfilePictureColor = FBProfilePicture.color;
defaultAvatarColor = DefaultAvatar.color;
fbProfilePictureColor.a = 0f;
FBProfilePicture.color = fbProfilePictureColor;
}
void Start()
{
//StartCoroutine (FbLoginWait ());
}
IEnumerator FbLoginWait()
{
yield return new WaitUntil (()=> FB.IsInitialized);
FBLogin ();
}
//auto logare
private void InitCallBack()
{
if (FB.IsInitialized)
{
//UsernameText.text = "Initialized FB SDK";
FBErrorText.text = "fb is initialized";
FB.ActivateApp ();
FBErrorText.text = "fb activated app";
}
else
{
FBErrorText.text = "Failed to initialize the Facebook SDK";
Debug.Log ("Failed to initialize the Facebook SDK");
}
}
private void OnHideUnity(bool isGameShown)
{
if (!isGameShown)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
private void HandleLoginFB(bool IsLogged)
{
if (IsLogged)
{
FBErrorText.text = "user logged in";
Debug.Log ("User logged in");
}
else
{
FBErrorText.text = "user cancelled login";
Debug.Log ("User cancelled login");
}
}
private void AuthCallback(IResult result)
{
FBErrorText.text = "called authcallback";
HandleLoginFB (FB.IsLoggedIn);
FBErrorText.text = "handling fb";
if (FB.IsLoggedIn)
{
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
Debug.Log (aToken.UserId);
foreach (string perm in aToken.Permissions) {
Debug.Log (perm);
}
FB.API ("/me/picture?type=square&height=50&width=50", HttpMethod.GET, UpdateProfileImage);
FB.API ("me?fields=name", HttpMethod.GET, GetUsername);
FBErrorText.text = "fb is logged in";
}
else
{
FBErrorText.text = "fb is not logged in";
}
}
public void FBLogin()
{
FBErrorText.text = "called fblogin";
List<string> perms = new List<string> () { "public_profile, email", "user_friends" };
FBErrorText.text = "created perms";
FB.LogInWithReadPermissions (perms, AuthCallback);
FBErrorText.text = "ended fblogin";
}
private void UpdateProfileImage(IGraphResult result)
{
if(result.Texture != null)
{
FBProfilePicture.texture = result.Texture;
defaultAvatarColor.a = 0f;
fbProfilePictureColor.a = 255f;
DefaultAvatar.color = defaultAvatarColor;
FBProfilePicture.color = fbProfilePictureColor;
}
}
private void GetUsername(IGraphResult result)
{
FBUserName = result.ResultDictionary ["name"].ToString ();
UsernameText.text = FBUserName;
WelcomeMessage.text = FBUserName + "'s NET WORTH";
}
}
(or the same code here https://hastebin.com/osatiketij.cs)
The last error text on the screen which appears is : "created perms", so right before the FB.LogInWithReadPermissions method is called, no error text appears after that, but no login also.
Any idea what it might be ?
For everyone coming to this thread : how I manage to fix this was by using Android Studio to see with the Logcat why it doesn't work, and it was "missing" a class. For anyone coming here, I fixed it by adding "-keep class com.facebook.** {*;}" to my proguard-user file.
Overall skeleton of the app goes like this,
The app opens, let's the new user to register or already existing user to login goes into SQLite. Later I want to fetch data from/through the user's profile. Is it possible?
I have been looking into this AccessToken stuff offered by Fb but everything flows over my head.
Let's say the user logs in initially by registering. Later I'll let the user to type in what to search and I want to fetch those search result from Facebook, those search results should also be from the user's profile. All these should happen even without the user manually entering his Fb details, rather he's going to get a pop up dialog box that says Continue with Fb login something of that sort.
Facebook user details only access with Access Token which is generated by facebook. First need to create developer account then create app over developer account. App ID and Key need to put on your android app which you can get from facebook developer account after creating an app there. For more details check out its official documentation.
I assume you question states that you want to get the user detail from facebook without getting logged in you app.if that is the case yes it is possible. because getting details from facebook is a different process and you getting logged in your app is a different one.
private Context context;
private Fragment mFragment;
private ShareDialog mShareDialog;
/**
* #param context
*/
public FacebookLoginManager(Context context, Fragment fragment) {
this.context = context;
this.mFragment = fragment;
}
/**
* #param context
*/
public FacebookLoginManager(Context context) {
this.context = context;
}
/**
* The purpose of this method is to initialize the facebook sdk
*
* #param callbackManager
*/
public void initializedFacebook(CallbackManager callbackManager) {
if (FacebookSdk.isInitialized()) {
performFacebookLogin(callbackManager);
} else {
FacebookSdk.sdkInitialize(getApplicationContext());
performFacebookLogin(callbackManager);
}
}
/**
* The purpose of this method is to initialize the facebook data from the server
*
* #param callbackManager
*/
private void performFacebookLogin(CallbackManager callbackManager) {
try {
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
final String accessToken = "" + loginResult.getAccessToken().getToken();
Log.e("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
final FacebookResponseBean mFacebookResponseBean = new FacebookResponseBean();
if (object != null) {
if (object.has(FacebookConstants.ID) && object.get(FacebookConstants.ID) != null) {
mFacebookResponseBean.setUserId(object.get(FacebookConstants.ID) + "");
}
if (object.has(FacebookConstants.NAME) && object.get(FacebookConstants.NAME) != null) {
mFacebookResponseBean.setName(object.get(FacebookConstants.NAME) + "");
}
if (object.has(FacebookConstants.EMAIL) && object.get(FacebookConstants.EMAIL) != null) {
mFacebookResponseBean.setEmail(object.get(FacebookConstants.EMAIL) + "");
}
if (object.has(FacebookConstants.GENDER) && object.get(FacebookConstants.GENDER) != null) {
mFacebookResponseBean.setGender(object.get(FacebookConstants.GENDER) + "");
}
if (object.has(FacebookConstants.BIRTHDAY) && object.get(FacebookConstants.BIRTHDAY) != null) {
mFacebookResponseBean.setBirthday(object.get(FacebookConstants.BIRTHDAY) + "");
}
if (object.has(FacebookConstants.AGE_RANGE) && object.get(FacebookConstants.AGE_RANGE) != null) {
mFacebookResponseBean.setAgerange(object.get(FacebookConstants.AGE_RANGE) + "");
}
if (object.has(FacebookConstants.TIMEZONE) && object.get(FacebookConstants.TIMEZONE) != null) {
mFacebookResponseBean.setTimezone(object.get(FacebookConstants.TIMEZONE) + "");
}
if (object.has(FacebookConstants.VERIFIED) && object.get(FacebookConstants.VERIFIED) != null) {
mFacebookResponseBean.setVerified(object.get(FacebookConstants.VERIFIED) + "");
}
if (object.has(FacebookConstants.UPDATED_TIME) && object.get(FacebookConstants.UPDATED_TIME) != null) {
mFacebookResponseBean.setUpdatedtime(object.get(FacebookConstants.UPDATED_TIME) + "");
}
if (object.has(FacebookConstants.LOCALE) && object.get(FacebookConstants.LOCALE) != null) {
mFacebookResponseBean.setLocale(object.get(FacebookConstants.LOCALE) + "");
}
if (object.has(FacebookConstants.FIRST_NAME) && object.get(FacebookConstants.FIRST_NAME) != null) {
mFacebookResponseBean.setFirstName(object.get(FacebookConstants.FIRST_NAME) + "");
}
if (object.has(FacebookConstants.LAST_NAME) && object.get(FacebookConstants.LAST_NAME) != null) {
mFacebookResponseBean.setLastName(object.get(FacebookConstants.LAST_NAME) + "");
}
if (object.has(FacebookConstants.PICTURE) && object.get(FacebookConstants.PICTURE) != null) {
JSONObject picture = object.getJSONObject(FacebookConstants.PICTURE);
JSONObject data = picture.getJSONObject("data");
if (data.has("url") && data.get("url") != null) {
mFacebookResponseBean.setPhotolink(data.get("url") + "");
Logger.error("image", data.get("url") + "");
}
}
}
if (context instanceof LoginActivity) {
((LoginActivity) context).setFaceBookResponse(mFacebookResponseBean);
} else if (context instanceof RegistrationActivity) {
((RegistrationActivity) context).setFaceBookResponse(mFacebookResponseBean);
} else if (context instanceof UserProfileActivity) {
((UserProfileActivity) context).setFaceBookResponse(mFacebookResponseBean);
} else if (mFragment != null && mFragment instanceof FragmentLinkAccounts) {
((FragmentLinkAccounts) mFragment).setFaceBookResponse(mFacebookResponseBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", FacebookConstants.PERMISSION);
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
exception.printStackTrace();
if (exception instanceof FacebookAuthorizationException) {
if (AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The purpose of this method is to share the image and content on facebook
*
* #param activity
* #param manager
* #param image
* #param isShareRequest
* #param id
*/
public void shareContent(final Activity activity, CallbackManager manager, View image, final boolean isShareRequest, int id) {
mShareDialog = new ShareDialog(activity);
String time = GlobalAcess.getDateTime();
mShareDialog.registerCallback(manager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
if (isShareRequest)
ToastMessage.getInstance(activity).showLongMessage(activity.getString(R.string.msg_request_posted));
else
ToastMessage.getInstance(activity).showLongMessage(activity.getString(R.string.msg_trip_posted));
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
error.printStackTrace();
}
});
String url = "";
if (isShareRequest) {
url = NetworkConstants.SHARE_REQUEST_URL + "id=" + id + "?t=" + time;
} else {
url = NetworkConstants.SHARE_TRIP_URL + "id=" + id + "?t=" + time;
}
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(url))
.build();
mShareDialog.show(content);
}
}
}
//
I did the login using Google Identity Toolkit, I have noticed that the class GitkitUser.UserProfile retrieves the photo url, but is too small. The google documentation do not say anything about photo size.
https://developers.google.com/identity/toolkit/android/reference/com/google/identitytoolkit/GitkitUser.UserProfile.html#getPhotoUrl()
For example with Facebook login, the getPhotoUrl() method returns:
https://scontent.xx.fbcdn.net/hprofile-xap1/v/t1.0-1/p50x50/12651146_10208004779813340_3124516205553866664_n.jpg?oh=efa817d10aaf9d184a767bae81a71071&oe=576850AD
For example with Gmail login, the getPhotoUrl() method returns:
https://lh6.googleusercontent.com/-5XFRyKHh7Os/AAAAAAAAAAI/AAAAAAAABIo/Trf7GjTnFec/s96-c/photo.jpg
Deleting /s96-c (or replace to /s200-c) in the Gmail photo url appears big, but I need a workaround to Facebook photo.
The solution for android was obtain the federatedId and after that call:
http://graph.facebook.com/{federatedId}/picture?type=large
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
client = GitkitClient.newBuilder(this, new GitkitClient.SignInCallbacks() {
#Override
public void onSignIn(IdToken idToken, GitkitUser user) {
DataStorage.getInstance().setLastToken(idToken.getTokenString());
Configuration config = Configuration.fromMetaData(AppInfo.getAppInfo(LoginActivity.this).metaData);
ApiClient apiClient = new ApiClient(config.getApiKey(), AppInfo.getAppInfo(LoginActivity.this), config.getServerWidgetUrl());
final GetAccountInfo.Request request = apiClient.newGetAccountInfoRequest(idToken);
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
GetAccountInfo.Response accountInfo = request.execute();
JSONArray users = accountInfo.getJsonResponse().optJSONArray("users");
JSONObject user = users == null ? null : users.optJSONObject(0);
String email = user == null ? null : user.optString("email");
if (email != null) {
JSONArray providerUserInfo = user.optJSONArray("providerUserInfo");
if (providerUserInfo != null && providerUserInfo.length() != 0) {
for (int i = 0; i < providerUserInfo.length(); ++i) {
JSONObject userInfo = providerUserInfo.optJSONObject(i);
if (userInfo != null) {
try {
String userInfoString = userInfo.getString("federatedId");
if(userInfoString.contains("facebook.com")) {
int lastSlash = userInfoString.lastIndexOf("/");
if(lastSlash != -1) {
String federatedIdFacebook = userInfoString.substring(lastSlash + 1, userInfoString.length());
Log.i("federatedIdFacebook", federatedIdFacebook);
}
break;
}
} catch (JSONException e) {
Log.e("LoginActivity", e.getMessage());
}
}
}
}
}
return null;
}
}.execute();
}
#Override
public void onSignInFailed() {
Toast.makeText(LoginActivity.this, "Sign in failed", Toast.LENGTH_LONG).show();
}
}).build();
}
You could use the idToken to get the User's identifier at IDP (facebook id).
See users[].providerUserInfo[].federatedId at https://developers.google.com/identity/toolkit/web/reference/relyingparty/getAccountInfo
And then use the facebookId to get the large account picture, with
http://graph.facebook.com/{facebookId}/picture?type=large
I'm trying to implement InAppBilling (Android) subscription. I'm trying to use component (Xamarin.InAppBilling). I can purchase the subscription but when I trying to get purchases item by subcription, nothing returns for me. How are you implementing IAB (Android) subscription? Someone have a example?
[EDITED]
public class PurchaseManager : IPlayListener
{
MLActivity2 _act;
BillingService m_service;
int m_requestId = 1;
//String _productId = "subscription.test";
//atributos IAB
String _publicKey = "";
String _productId = "";
public bool _premium;
public PurchaseManager(MLActivity2 act)
{
_act = act;
IniciarPurchase();
}
public void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
m_service.HandleActivityResult(requestCode, (int)resultCode, data);
}
public void Finalizar()
{
if (m_service != null)
m_service.Dispose();
}
//Process referentes a IAB
private void IniciarPurchase ()
{
Security.ExpectSignature = false;
m_service = new BillingService(_act, this, _publicKey);
var connTask = m_service.Connect();
//Load inventory on start-up
connTask.ContinueWith(t =>
{
if (t.Result)
{
VerificarAssinatura();
}
});
}
public void Connected()
{
}
public void Disconnected()
{
}
private void VerificarAssinatura()
{
//Existing purchases
m_service.SendRequest<GetPurchasesResponse>(new GetPurchases(play.billing.v3.Consts.ITEM_TYPE_SUBS, m_requestId++)).ContinueWith(tGP =>
{
_act.RunOnUiThread(() =>
{
if (tGP.Result.Success)
{
foreach (Purchase item in tGP.Result.PurchasedItems)
MLActivity2.UiShowToast(_act, "", "Order id" + item.OrderId, true);
if (tGP.Result.PurchasedItems.Count == 0)
MLActivity2.UiShowToast(_act, "", "nao tem item", true);
}
else
MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "Purchases request failure. Error: " + tGP.Result.Message, false);
});
});
//Existing purchases
m_service.SendRequest<GetPurchasesResponse>(new GetPurchases(play.billing.v3.Consts.ITEM_TYPE_INAPP, m_requestId++)).ContinueWith(tGP =>
{
_act.RunOnUiThread(() =>
{
if (tGP.Result.Success)
{
foreach (Purchase item in tGP.Result.PurchasedItems)
MLActivity2.UiShowToast(_act, "", "Order id" + item.OrderId, true);
if (tGP.Result.PurchasedItems.Count == 0)
MLActivity2.UiShowToast(_act, "", "nao tem item", true);
}
else
MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "2222Purchases request failure. Error: " + tGP.Result.Message, false);
});
});
}
public void JaPossuiAssinatura()
{
MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.assinatura_japossui_titulo), _act.GetString(Resource.String.assinatura_japossui_texto), true);
}
public void Comprar ()
{
var req = new Buy(_productId, m_requestId++);
m_service.SendRequest<Response>(req).ContinueWith(t =>
{
_act.RunOnUiThread(() =>
{
if (t.Result.Success)
VerificarAssinatura();
else
MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "Purchases request failure. Error: " + t.Result.Message, false);
});
});
}
void CompraComSucesso()
{
MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.assinatura_sucesso_titulo), _act.GetString(Resource.String.assinatura_sucesso_texto), true);
}
void ErroNaCompra()
{
//MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), _act.GetString(Resource.String.assinatura_problema), false);
}
}
Adding a code snippet always helps to get the right answer faster. Don't forget it next time ;)
When you query purchases, make sure you use correct item type: subscription (and not the product). If you have both, you have to do two queries - one for products and one for subscriptions.