How to get Access and Refresh Token while allowing google app by user - google-apps

I created a google app and in Google app marketplace sdk api filled all the required details and added scopes what i required. And created a webapp in chrome developer dashboard and submitted the app for testing. While first time installing the app from marketplace it get permission from user for the scopes i added in the google app marketplace sdk api. After i click allow it will just install the app. Here how can i retrieve Accesstoken and refresh token or Authorization code while user clicks allow?
I manually get Auth code using this api call:
""https://accounts.google.com/o/oauth2/auth?client_id=CLIENTID &redirect_uri=REDIRECT_URI&scope=email+profile+https://www.googleapis.com/auth/drive&response_type=code&access_type=offline""
In the redirected uri collects access and ref token using this api call:
String accessTokenUrl = "https://accounts.google.com/o/oauth2/token";
String accessTokenUrlParameters = "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri="
+ redirectUri + "&grant_type=authorization_code&code=" + code;
String accessToken = null;
String refreshToken = null;
try {
HttpClient hc = new HttpClient();
hc.setURL(accessTokenUrl);
hc.setHeader("Content-Length", "" + Integer.toString(accessTokenUrlParameters.getBytes().length));
hc.doPost("application/x-www-form-urlencoded; charset=UTF-8");
}
In this type i get both access and refresh tokens but while installing app for first time how do i get access token using that permissions and scopes. The consent screen while installing app has url like this.
https://accounts.google.com/o/oauth2/auth?client_id=1234567890-1od573nk87eq712l7suam1hu9upa8tm2.apps.googleusercontent.com&origin=https%3A%2F%2Fapis.google.com&authuser=0&login_hint=abcdefghijk#gmail.com&response_type=token&redirect_uri=postmessage&hl=en&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
but it doesnt have redirect uri and all. How to get access token while allowing this consent screen.

Related

Refresh Facebook access token

I am using the Facebook Login for Devices API to authenticate a device that only has a display where I want to display Page information.
I am able to authenticate fine and get an access token and display page information like I want but how do I refresh the access token that was given to me when the user put in the device code to allow the device by going to facebook.com/devices? I cant have the user do this every 60 days or whenever the token expires.
I have not found any documentation about how to refresh them other than saying that the native sdk's do it automatically. Can anyone point me to documentation or something where I can refresh access tokens?
After looking through the Facebook Sdk for Android I found that its a simple graph api call that you can make to extend an access token
private static GraphRequest createExtendAccessTokenRequest(AccessToken accessToken, GraphRequest.Callback callback) {
Bundle parameters = new Bundle();
parameters.putString("grant_type", "fb_extend_sso_token");
parameters.putString("client_id", accessToken.getApplicationId());
return new GraphRequest(
accessToken, TOKEN_EXTEND_GRAPH_PATH, parameters, HttpMethod.GET, callback);
}
Where TOKEN_EXTEND_GRAPH_PATH is oauth/access_token

Connect to Facebook Event Search via Google App Engine and restfb

I want to try Facebook Event search by using restfb - see http://restfb.com . My app resides on google app engine. Unfortunately I am not able to figure out what methods I need to call and how to get the right token (accessToken), which is needed for authentication and for the query.
Here is my snippet:
ScopeBuilder scopeBuilder = new ScopeBuilder();
scopeBuilder.addPermission(UserDataPermissions.USER_STATUS);
scopeBuilder.addPermission(UserDataPermissions.USER_ABOUT_ME);
String accessToken = ""; // How to get?
FacebookClient facebookClient = new DefaultFacebookClient(accessToken, Version.LATEST);
Page page = facebookClient.fetchObject("cocacola", Page.class);
System.out.println("Page likes: " + page.getLikes());

Mobile Services Authentication with Facebook always gives Unauthorized error

I've been trying to create unity application and connect it with mobile services. I'm trying to do FB authentication and here's what I've done on my checklist :
Registered my FB App Id and App secret properly on mobile services.
For testing I use FB tools to generate access token for now.
Here's my code to login to FB using Restsharp :
// token is hardcoded with access token from FB tools for test
AuthenticationToken authToken = CreateToken(provider, token);
_LoginAsyncCallback = callback;
var path = "/login/" + provider.ToString().ToLower();
var baseClient = new RestClient(_baseEndPoint);
var request = new RestRequest(path, Method.POST);
var json = SerializeObject(authToken);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
var handle = baseClient.ExecuteAsync<MobileServiceUser>(request, LoginAsyncHandler);
provider is facebook and my post is "access_token":"XXXXXXXX" so this should be correct right?
I always get this error :
Rest Response:{"code":401,"error":"Error: The Facebook Graph API access token authorization request failed with HTTP status code 400"}
I've tried many FB app settings and generate new access tokens from FB tools, etc but nothing seems to work. I'm at my wits end on this

Error in getting active access token from FB

I am having the following code to access FB graph API.
var fb1 = new FacebookClient();
dynamic result = fb1.Get( "oauth/access_token", new
{
client_id = "523408...",
client_secret = "25bd19645....",
grant_type = "client_credentials");
}
var apptoken = result.access_token;
FacebookClient fb = new FacebookClient(apptoken);
dynamic FriendList = fb.Get("me");
string t = FriendList.ToString();
Console.WriteLine(t);
Console.ReadKey();
}
But when I execute it, it is giving this error "(OAuthException - #2500) An active access token must be used to query information about the current user."
Can somebody please tell me how to get active access token using C# code?
The "User" generates the Access Token, not the server.
This page gives you a way to do it thru Facebook CSharp SDK:
I personally use Facebook JavaScript SDK for the Login and pass the Access Token to the server to make calls.
http://facebooksdk.net/docs/web/permissions/
This page gives you information about different authentication workflows:
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.0
https://developers.facebook.com/docs/facebook-login/access-tokens/
Hope it helps.
I think access token and user (me) they're not the same person. Check Access token and user. I think we all have this problem.

Facebook Graph API get USER token instead of APP token (phonegap)

I'm trying to implement facebook connect in Adobe PhoneGap through the Graph API but apparently I'm getting back an "App" token instead of a "User" token. This causes for my app to disallow any additional users connecting onto it, which is not the point of course.
I have notice this as no matter what user I use for logging in, the access_token returned is always the same.
I'm using the following URL to authenticate:
var authorize_url = "https://graph.facebook.com/oauth/authorize?";
authorize_url += "client_id=" + fb_clientid;
authorize_url += "&redirect_uri=" + fb_redirect_uri;
authorize_url += "&display=" + fb_display;
authorize_url += "&scope=publish_stream"
And to get the authorisation token:
https://graph.facebook.com/oauth/access_token?client_id='+fb_clientid+'&client_secret='+fb_secret+'&code='+fbCode+'&redirect_uri=http://www.facebook.com/connect/login_success.html'
I presume the problem lies with the second url (secret being passed indicates it's an app token) but then how do I get the user token?
Use the Facebook Connect plugin, instead of crafting the login procedure manually. It provides better usability as it is integrated with the Facebook native application or the iOS 6 Facebook functionality.