I'm developing an application using Xamarin.Android.
I need to enable login with Facebook and Google and I choose to use the Xamarin.Auth component.
I got it work with facebook but I have some issue with Google.
This is my code:
var auth = new OAuth2Authenticator (
clientId: "myId.apps.googleusercontent.com",
clientSecret: "mysecred",
scope: "openid",
authorizeUrl: new Uri ("https://accounts.google.com/o/oauth2/auth"),
redirectUrl: new Uri ("myredirect:oob"),
accessTokenUrl: new Uri ("https://accounts.google.com/o/oauth2/token"),
getUsernameAsync: null
);
activity.StartActivity (auth.GetUI (activity));
auth.Completed += (sender, e) => {
Console.WriteLine (e.IsAuthenticated);
};
Is this way the Google Activity is displayed, and I can insert my Username and password. But when I click the google login button I get a message like this:
google auth please copy this code switch to your application and paste it there [code]
What I have to do with that code? I just need to get the user name/lastname/email/id.
Thanks a lot!
Bellow steps worked for me.
1.register in google developer console as webapplication instead of installed application(android)* provide the redirect url with valid url ("http://abcd.com/xyz.aspx") same should be used in the application code.
2.on authentication complete it will return access_token
3.by using the access_token make the REST request to get user complete information (https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessTokenValue + “.)
4.Deserialize the json response to get information in object.
check the source code :http://appliedcodelog.blogspot.in/2015/08/login-by-google-account-integration-for.html
Related
Never really worked with OAuth, trying to implement it now, I want to get access token and profile data from google and facebook. Using Xamarin.Auth.
With Facebook there're no problems, I specify "http://www.facebook.com/connect/login_success.html" as redirect url and after I login it goes back to the activity I was before.
However with Google it's not as smooth - couldn't find any similar to facebook login success pages, somewhere found suggestion to use "https://www.googleapis.com/plus/v1/people/me" - added it to redirect url white list, however after sign in I would get "Redirect_url_mismatch" A native application: application nameaccording to their documentation I should use "my.package.name:" and again I added that to redirect url white list, attempted to sign in, this time after sign in screen I get to second screen where I need to confirm read permissions and after that I get very short error something like "com.my.package:/?oauthparameterX=value1...." and get redirected to permission screen again.
Here's my complete OAuth2Authenticator:
var auth = new OAuth2Authenticator(
clientId: SocialIds.GooglePlusId,
clientSecret: SocialIds.GooglePlusSecret,
scope: OAuthUrl.GoogleScope,
authorizeUrl: new Uri(OAuthUrl.GoogleAuthorize),
redirectUrl: new Uri("https://www.googleapis.com/plus/v1/people/me"),
accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
getUsernameAsync: null);
auth.AllowCancel = false;
urls:
public static string GoogleAuthorize = "https://accounts.google.com/o/oauth2/auth";
public static string GoogleScope = "https://www.googleapis.com/auth/userinfo.email";
public static string GoogleRedirect = "https://www.googleapis.com/plus/v1/people/me";
public static string GoogleUserInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token={0}";
In the above listed code, you are not giving the redirect uri instead you are giving the scope of google api. The purpose of redirect uri is to recieve the response from google api after authorization. The response should be a code. This response code is used for accessing the access_token,refresh_token, id_token etc. So you have to recieve this code in your project side.For this purpose , the redirect uri is used.
Go to your google console, create project, add credentials, then you will be redirect to a page conatains,
You can find the authorized redirect url. Give the url , then configure your code with new redirect url . Everything will be fine after this.
I am posting to user's wall using following code using restFB.
String myAccessToken = null;
String appId="my app id ......";
String appSecret="my app secret";
// facebook user id of the user on whose wall to post
String userId="user id ...";
DefaultFacebookClient dfc = new DefaultFacebookClient();
AccessToken accessToken = dfc.obtainAppAccessToken(appId,appSecret);
myAccessToken = accessToken.getAccessToken();
FacebookClient facebookClient = new DefaultFacebookClient(myAccessToken);
FacebookType publishMessageResponse = facebookClient.publish(userId+"/feed", FacebookType.class, Parameter.with("message", "test"));
System.out.println("Published message ID: " + publishMessageResponse.getId());
All is working fine.
But now I want to include user's facebook first name in the post. Is there any way to do so? I dont want to generate yet another request to facebook for getting the username, as I am already generating 2 requests for getting user id(from redirect url). Is there any mechanism that graph api itself replaces some kind of tag in the post text with user name.
E.g I send the following text to post on user wall:
#username# is doing something...
And it is replaced by facebook graph api
Mark is doing something....
I dont want to generate yet another request to facebook for getting the username, as I am already generating 2 requests for getting user id(from redirect url).
No you can't. You have to make the request to fetch the name. /me?fields=name
But why does it matter, it'll take very less time to query this.
I am trying to retrieve an authorization code to use in my server side following these instructions. The client ID of the server shares the same project of my Android Client ID.
I can retrieve the access token, but not the authorization code:
String mScopes = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";
//String mScopes = "oauth2:server:client_id:314345278679.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";
Bundle mAppActivities = new Bundle();
mAppActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES, "");
String authorizationToken = GoogleAuthUtil.getToken(LoginActivity.this, mPlusClient.getAccountName(), mScopes, mAppActivities);
Uncommenting to use the alternative scope (to retrieve an authorization code instead of a token) produces the following error:
I/GLSUser (15293): GLS error: INVALID_SCOPE my_email_address#company.com oauth2:server:client_id:314345278679.apps.googleusercontent.com:api_scope:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
Any ideas?
You're missing https://www.googleapis.com/auth/plus.login from your scopes. I tried your code and get INVALID_SCOPE without plus.login, but it works fine with.
I have a problem. I can bring name, gender(public profile). but I can't bring user_birthday ,email, offline_access.
Problem is as follow.
Should not pass a read permission (user_birthday) to a request for publish or manage authorization.
Should not pass a read permission (email) to a request for publish or manage authorization.
Should not pass a read permission (offline_access) to a request for publish or manage authorization.
request and bring Facebook data source
try{
json = Util.parseJson(mFacebook.request("me"));
uuserName = json.getString("name");
userSex = json.getString("gender");
userBirth = json.getString("user_birthday");
userEmail = json.getString("email");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login source
String[] sPermissions = {"user_birthday", "email", "offline_access"};
mFacebook.authorize(this, sPermissions, new AuthorizeListener());
try this code:
userBirth = json.getString("birthday");
The messages that you are getting are warnings and not errors.
It is happening becuase you are using the Facebook Android SDK 2.0 , which has been made deprecated after the release of SDK v3.0.
In the latest SDK, Util class is no longer used in the same way and permissions are requested in a different manner.
Facebook.request("me") has also become deprecated as now all the data can be fetched on user login using widget.Facebook.loginButton.
Try and go through the documentation of the updated SDK and the warnings should stop bothering you.
And coming to the actual problem why you are not able to get the birthday is because of the wrong parameter you are trying to access in the JSON data.
You need to use json.getString("birthday"), according to the documentation.
I am implementing OAUTH in android using appcelerator. login page is redirecting to response page with token appended to query string e.g. http://mysite.com?access_token="token"
So I need to fetch this query string using appcelerator.
I have used Ti.App.getArguments()
but this api is only working for ios not for android.
Is there any api available for fetching query string in appcelerator that works in android??
Add an event listener to the webview like so:
webview.addEventListener('load', function(e){
var url = e.url.split('?');
var urlParams = url[1];
});
The variable urlParams will contain a string that looks like param1=value1¶m2=value2 so from there you just need to split them up and find the access_token param.