Google app engine Endpoints, Objectify Register - google-cloud-endpoints-v2

I start google app engine with endpoints, I use objectify for persistence and I have an little question.(I hope !)
All my apiMethod start like it :
#ApiMethod(path = "getAccount", httpMethod = HttpMethod.GET)
public Account getAccount(#Named("idPlayer") long idPlayer) {
ObjectifyLoader.loadEntity();
ObjectifyLoader.loadEntity(); is just a method like it
public static void loadEntity(){
ObjectifyService.register(Account.class);
ObjectifyService.register(WeaponsAccount.class);
}
I got the impression that I register my class et each call to the api.
A tips for load only one time?
Thanks you for reading.
Fabiitch

You want to use a ServletContextListener. Check out this App Engine oage for an example.

Related

Facebook OAuth stopped working suddenly

I noticed yesterday that my Facebook login for my website has stopped working.
This has been working great for the last 2 months, as far as I am aware I have not changed anything. I have tried everything I can on links such as: - as well as many more...
ASP.NET MVC5 OWIN Facebook authentication suddenly not working
I have noticed that the Stack Overflow Facebook auth has also stopped working.
Has anyone else noticed this and found any solution? It's worth noting I am using azure app services to host. But this issue is also found when I am using localhost.
My current setup looks like this...
in Startup.Auth.cs
var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
AppId = "xxxxxxxxxxxxx",
AppSecret = "xxxxxxxxxxxx"
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);
In the following method, loginInfo is null every time.
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
I also added a session "WAKEUP" from a different post suggestion, fb auth failed once before and this fixed the issue this time, but it has come back.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
Session["WAKEUP"] = "NOW!";
// Request a redirect to the external login provider
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
As RockSheep explained. Facebook dropped the support vor API v2.2. You need to update your OWIN nuget packages.
You can find the issue on github (from the Katanaproject).
Ensure to activate pre releases in your nuget manager, than you are able to update the nuget packages to version v3.1.0-rc1. But beware: After the update, you need to test your login carefully (maybe you also have other authentication providers like Microsoft or Google, you should test them as well).
Technical
The Api changed the version number to v2.8 and the return value from the API is now in JSON-Format and no longer escaped in the URI. The 'old' OWIN packages can not handle this changes.
[Oauth Access Token] Format - The response format of
https://www.facebook.com/v2.3/oauth/access_token returned when you
exchange a code for an access_token now return valid JSON instead of
being URL encoded. The new format of this response is {"access_token":
{TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this
update to be compliant with section 5.1 of RFC 6749.
Here you can find the code-changes on GitHub for further informations and better understanding.
A lot of people started having trouble after yesterday. This is due to Facebook dropping support for v2.2 of their API. For some reason their system still redirects auth calls that don't use a version number to the 2.2 API. A quickfix is to ensure that the API version gets sent with the API call.
Starting at v2.3 Facebook also started returning JSON objects. So make sure to change that in the code as well.
I had the same issue, found solution here Fix facebook oauth 2017
Basically, you need to extend HttpClientHandler and decode JSON response instead of body
Here is a solution for those who are using scribe java.
public Token extract(String response)
{
Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
JSONObject obj = new JSONObject(response);
return new Token(obj.get("access_token").toString(), EMPTY_SECRET, response);
}
Create a new class and set the extractor to JSON.
import org.scribe.builder.api.DefaultApi20;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.extractors.JsonTokenExtractor;
import org.scribe.model.OAuthConfig;
public class FaceFmApi extends DefaultApi20 {
#Override
public String getAccessTokenEndpoint()
{
return "https://graph.facebook.com/oauth/access_token";
}
#Override
public AccessTokenExtractor getAccessTokenExtractor()
{
return new JsonTokenExtractor();
}
#Override
public String getAuthorizationUrl(OAuthConfig config) {
return null;
}
}
and inject your newly created class as below. Then getAccessToken() will work as expected.
public OAuthService getService() {
return new ServiceBuilder().provider(FaceFmApi.class)
.apiKey(config.getApiKey()).apiSecret(config.getApiSecret())
.callback(config.getCallback()).build();
}

Securing Babaganoush Web Api

Loving the Web API and have created some custom ones for our dynamic module. Is there a way we can secure all the web services rather than just the custom ones we have created?
Thanks.
When inheriting from BaseDynamicController<YouCustomModel>, you can override any of the services like this:
public override IEnumerable<YouCustomModel> Get(int take = 0)
{
if (/* Not Authenticated */)
throw new System.Security.SecurityException("Not authorized to access content");
return base.Get(take);
}
In a future release, we are looking to allow this to be controlled from a virtual method: https://babaganoush.uservoice.com/forums/259241-general/suggestions/6255143-secured-web-services-option-for-dynamic-content

EWS from WebSite using user credentials and windows authentication for intranet

I am trying to understand how I should configure a web site (ASP.Net) that need to pass the user credential and get some mail things
I get different kind of errors depending on the app pool configure,
What is the complete way to do it? I mean code + Configuration ;o)
I don't want to write the user and password but get them through the windows authentication
Thanks
code:
Service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
{
Url =
new Uri(
"https://xxx/yyy.asmx"),
//UseDefaultCredentials = true doesnt help
};
var view = new ItemView(1820);
// Find the first email message in the Inbox that has attachments. This results
FindItemsResults<Item> results = Service.FindItems(folderName, view);
//Here I get the error 401
Take a look at Get started with EWS Managed API client applications, it covers the basics to get you up and rolling. Note that when you use UseDefaultCredentials, you have to be on a domain joined machine, with the user logged in, and you must be targeting an on-prem server. We tested that scenario out and the following code works in that scenario.
using System;
using Microsoft.Exchange.WebServices.Data;
namespace HelloWorld
{
class Program
{
static void Main()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.UseDefaultCredentials = true;
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
//Replace the URL below with your own, but using AutoDiscover is recommended instead
service.Url = new Uri("https://computer.domain.contoso.com/EWS/Exchange.asmx");
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox,
new ItemView(1));
}
}
}
If that doesn't work for you, then you might want to try using impersonation. See this thread, where I think they've got a similar situation . More information about impersonation is on MSDN: Impersonation and EWS in Exchange.

accessing to google api in unity

I want to access to google's APIs in Unity3d. I want to use google plus API in my Unity3d application to have sharing feature in the app.
Please help me if you worked with google's APIs in Unity3d.
Thanks.
There is a detailed explanation of how to do this here for C#/.NET. You will at most have to make only minor changes to the code to make it work with Mono. One thing to note, is that you will have to include this code in order to tell Mono to allow the requests to the API. (source)
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class UnsafeSecurityPolicy {
public static bool Validator(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors policyErrors) {
//*** Just accept and move on...
Debug.Log ("Validation successful!");
return true;
}
public static void Instate() {
ServicePointManager.ServerCertificateValidationCallback = Validator;
}
}
You will have to call Instate before you start using the API.

Sending a file throught a Post with HTTPBuilder in Grails

I am developing a Facebook Application an i wanna send a video file from myServer to Facebbok User`s Timeline. This is the page with the explanation http://developers.facebook.com/blog/post/515/#video_upload but its a PHP code.
My app is on Grails, and im looking in HTTBuilder class but can't find a way to do this.
Do someone know how to do it?
If isnt possible to do this with HTTPBuilder, in my app, i am using Spring Social Facebook Plugin on Grais
I found the interface MediaOperations but i dont know how to use this interface and use the method postVideo to upload a Video.
Thanks!
Will try to help a bit. You may use MediaOperations interface for this operation. Spring Social Facebook Plugin configures a service called Facebook for you. You can use it via dependency injection.
Here is a simple code example:
import org.springframework.social.facebook.api.Facebook
class FacebookService {
Facebook facebook
def uploadVideo(String videoFileName, String title, String description) {
try {
def videoResource = new FileSystemResource(videoFileName)
facebook.mediaOperations().postVideo(videoResource, title, description)
return true
}
catch (Exception e) {
log.error("Error to upload video to facebook", e)
return false
}
}
}
The video is loaded from the file in the FS by specified file path/name from videoFileName variable. This means, user need to upload the video first, and code should save it to some file in FS first, then upload. Usually this is the best case, as video files are large. Maybe there is a sense to upload the video to facebook in separate thread and don't give the user to wait.