getDefaultGcsBucketName Response contained no data - google-app-engine

I try to call AppIdentityService#getDefaultGcsBucketName but it throws an exception: Response contained no data. According to the docs, the default bucket should be ready to use. Code:
//Field
private AppIdentityService service;
//in ctor
this.service = AppIdentityServiceFactory.getAppIdentityService();
//call inside a method
final String baseUrl = GCS_URL + "/" + service.getDefaultGcsBucketName() + "/" + fileName;

If you created your app prior to the 1.9.0 SDK release, you have to manually setup your default GCS bucket. Read this article for more information (I know its for PHP, but its applicable across all languages).

Related

AddHttpClient in Blazor Server Side

I'm trying to create an httpclient in Blazor Server side which would create the least amount of configuration effort every time I call my webapi.
Essentially I would like achieve the following:
Named HTTPClient I can automatically call when I call a function in my webapi.
The webapi requires a bearer token, which I get by calling AcquireTokenSilent
Would be great if I don't have to specify the httpclient when I call the api
The webapi has been added as a service reference, so there is scaffold classes created under the namespace myapp.server.api
To start this off, I created the following in startup:
services.AddHttpClient<myapp.server.api.swaggerClient>(c =>
{
c.BaseAddress = new Uri("https://api.myapp.com/");
AzureADB2COptions opt = new AzureADB2COptions();
Configuration.Bind("AzureAdB2C", opt);
IConfidentialClientApplication cca =
ConfidentialClientApplicationBuilder.Create(opt.ClientId)
.WithRedirectUri(opt.RedirectUri)
.WithClientSecret(opt.ClientSecret)
.WithB2CAuthority(opt.Authority)
.WithClientName("myWebapp")
.WithClientVersion("0.0.0.1")
.Build();
IHttpContextAccessor pp;
string signedInUserID = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
new MSALStaticCache(signedInUserID, pp.HttpContext).EnablePersistence(cca.UserTokenCache);
var accounts = cca.GetAccountsAsync().Result;
AuthenticationResult result = null;
result = cca.AcquireTokenSilent(opt.ApiScopes.Split(' '), accounts.FirstOrDefault()).ExecuteAsync().Result;
c.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
});
My hope is to be able to call my api in my views in this way:
myapp.server.api.swaggerClient t = new myapp.server.api.swaggerClient();
currentCount = t.WeatherForecastAsync().Result.FirstOrDefault().Summary;
calling a new instance of swaggerclient requires me to specify an httpclient, so my hopes is to inject the httpclient I am configuring on a global level for that type can be injected automatically.
The pieces I need help with:
Given that I have specified my httpclient scoped to a specific type, would it call automatically if I call a function in my webapi? (Does not seem to fire when debugging)
To get the bearer token, I need to get the current userID, which is in the authstateprovider... seeing that this is in Startup, is getting it from DI even possible?
Any easy way to inject the httpclient on the constructor of my webapi classes? would I be able to get the httpclient in the constructor so that I essentially have a parameterless constructor not asking for httpclient?
Concerning your first question, inject the Web API HttpClient like this in your view:
#inject myapp.server.api.swaggerClient MyClient
and then in the code block:
currentCount = MyClient.WeatherForecastAsync().Result.FirstOrDefault().Summary;
You should be able to debug the code inside AddHttpClient.

angularjs Rest API

I've read up many articles on angularjs and REST but could not found any solution. Through java script I am calling api method, which is routed method as:
[Route("api/Comments/{docId}/comment/{revId}/get/{size}/getNumber/{number}")]
[HttpPost]
public IEnumerable<Student> Get(int docId,int revId,int size, int number)
{
// loadienter code hereng list here
return list.ToList();
}
//java script code
var url='api/students/' + docId + '/student/' + revId + '/get/'+size+'/getNumber/'+number';
$http.get(url).success(function (response) {
if (callback) callback(response.result);
};
But the method in controller class is not executing..How to solve this issue? P
Please give me some suggestions..
If in your angular controller you have just that code for the call to the api it looks like when you are constructing your url variable you haven't included on the front of it what the domain is that you are hitting. such as http://localhost/ if your debugging it locally.
Also your route is defined to start as api/comments/ on your WebApi however in your javascript url you have api/students/

Blob Upload Hanging

I was wondering if anything had changed recently in relation to uploading blobs to appengine from external applications? What used to work perfectly only 3 months ago is now hanging when doing a http post to upload the blob.
The code (see below) which was working fine previously consists in fetching a pull queue from AppEngine (using the REST API), doing some stuff with this task received and then uploading back the result as a Blob on AppEngine. The url to upload the blob to is created by appengine using blobstoreService.createUploadUrl("/upload");
and is of the form:
http://myapp.appspot.com/_ah/upload/AMmfu6aAHnkuS4ngyRJDn7urFFZeBxb_-3P-r7RY9udMvRjLWkEZNJMgUX1DFczNVi-NhIxcFat2AEPXs2IRJ0AOmznSMgcrCKmL7mGAmS7nqtr-UyYFkglD88BwCfzIui9M2yez7DSQ/ALBNUaYAAAAAUGRlEwpeGEc5ozp8Z8sDO33qgCi2AiIE/
I had a look at the logs on AppEngine and it seems like the servlet in charge of /upload isn't being triggered.
I'm honestly out of ideas at this stage, any help would be greatly appreciated ! :-)
Cheers,
Killian
public boolean uploadAsBlob(String dataToWrite, String uploadURL) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFileLocation));
bufferedWriter.write(dataToWrite);
bufferedWriter.newLine();
bufferedWriter.close();
MultipartEntity entity = new MultipartEntity();
entity.addPart(blobFileName, new FileBody(new File(tempFileLocation)));
HttpPost method = new HttpPost(uploadURL);
method.setEntity(entity);
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
//It hangs at the following line!
HttpResponse response = httpclient.execute(method);
if (response.getStatusLine().getStatusCode() == 200) {
logger.info("Uploaded blob to url: " + uploadURL);
return true;
} else {
logger.warning("Couldn't upload blob to url: " + uploadURL);
}
} catch (Exception e) {
logger.warning("Exception " + e.getMessage() + " occured while uploading blob to url:" + uploadURL);
logger.warning("Couldn't upload blob to url: " + uploadURL);
}
return false;
}
I have found that GAE has recently started to keep any GET parameters when invoking blobstoreService.createUploadUrl(). In my case:
http://www.myapp.com/BG?_=1354631578951
With this (unexpected) parameter, the created URL was:
http://www.myapp.com/_ah/upload/?_=1354631578951/AMmfu6YgVPoJzWXdbf70k6J0zdjEeRnnRJ2PYCb3Jgdwk3SqmKEnFyKgy_17CKwiqbC2HyO-FlPVX-C53W0LjHSywaq7YmLegD97uU-GrpWRdBdWbfKf0Dk/ALBNUaYAAAAAUL4L8iDS5E99f3Wky2p59wWpCD84AqoP/
Notice that the '_' parameter is still there. Removing the parameter (or maybe moving from GET to POST) fixed the problem.

Gae MapReduce. How to send post request programmatically

I am trying to send post request to mapreduce in GAE.
Google provided
private String generateHtml(String configXml) {
return "<html>"
+ "<body>"
+ "<form action=\"/mapreduce/start\" method=\"POST\">"
+ "<textarea name=\"configuration\" rows=20 cols=80>"
+ configXml
+ "</textarea>"
+ "<input type=\"submit\" value=\"Start\">"
+ "</form>";
}
How I configure:
String configString = ConfigurationXmlUtil.convertConfigurationToXml(config);
String body = "configuration=" + configString;
Doing so I get null pointer exception when server tries to get parameter "configuration";
How should I build request so it would be identical to the html scenario with textarea?
Here's how I did it once using TaskQueue:
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
import com.google.appengine.api.taskqueue.TaskOptions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Mapper;
//... other imports
// mapreduce config
Configuration conf = new Configuration(false);
conf.setClass("mapreduce.map.class", YourMapperClass.class, Mapper.class);
conf.setClass("mapreduce.inputformat.class", DatastoreInputFormat.class, InputFormat.class);
// setup callbacks if you need to know when the job is done.
//conf.set(AppEngineJobContext.DONE_CALLBACK_URL_KEY, "/job-done-callback");
// you can also specify a queue. Defaults to "default" :)
//conf.set(AppEngineJobContext.CONTROLLER_QUEUE_KEY, "mrqueue");
//conf.set(AppEngineJobContext.DONE_CALLBACK_QUEUE_KEY, "mrqueue");
//conf.set(AppEngineJobContext.WORKER_QUEUE_KEY, "mrqueue");
conf.set(DatastoreInputFormat.ENTITY_KIND_KEY, 'YourEntityKind');
String xml = ConfigurationXmlUtil.convertConfigurationToXml(conf);
TaskOptions opts = withUrl("/mapreduce/start")
.param("configuration", xml)
.method(TaskOptions.Method.POST)
.header("X-Requested-With", "XMLHttpRequest");
// use our dedicated dbm4g queue
// this needs to be configured by a user
QueueFactory.getQueue("mrqueue").add(opts);
I used a non default queue to make sure my MapReduce jobs won't hit limits too much. But you can safely use the default queue too.

Retrieve salesforce instance URL instead of visualforce instance

From a visualforce page, I need to retrieve our organization's salesforce instance's URL, and not the visual force URL.
For example I need https://cs1.salesforce.com instead of https://c.cs1.visual.force.com
Here's what I've tried so far and the outcome I got:
Accessed the Site global variable from the VF Page:
<apex:outputText value="{!$Site.Domain}" /> returns null
Sidenote: Everything in $Site.xxx seems to return null.
From the Apex controller:
public String getSfInstance()
{
return ApexPages.currentPage().getHeaders().get('Host');
}
and
public String getSfInstance()
{
return URL.getSalesforceBaseUrl().toExternalForm();
}
returns c.cs1.visual.force.com and https://c.cs1.visual.force.com, respectively.
Question: How do I retrieve what I want: https://cs1.salesforce.com?
Here's something that I used within my Apex Trigger
System.URL.getSalesforceBaseUrl().getHost().remove('-api' );
This gives me proper URL
This is a known issue, the URL.getSalesforceBaseUrl() should provide this information but it does not. However in reality this has very limited functional impact.
Their instance and apex domains are interchangeable in the sense that requesting a URL that does not belong to one gets redirected to the other.
for example if you seek /apex/myPage from cs1.salesforce.com you'll get redirected to c.cs1... and vise versa requesting /ID from apex domain will get you redirected to instance domain (unless detail action has been overridden)
If this does not help you there is one workaround, albeit very ugly :) create a custom object to store the base url and create before insert/update trigger which will set the baseURL field to URL.getSalesforceBaseUrl().toExternalForm(). Apparently trigger is the only place on the platform where this will work (aside from execute anonymous which is not of much use). When setting up the app insert something into that table and later use SOQL to retrieve base url.
Here is an Apex property that you can throw into a Utility class that will reliably return the instance for your org. Using this, you can easily construct your organization's Salesforce URL by appending ".salesforce.com" to the Instance:
public class Utils {
// Returns the Salesforce Instance that is currently being run on,
// e.g. na12, cs5, etc.
public static String Instance {
public get {
if (Instance == null) {
//
// Possible Scenarios:
//
// (1) ion--test1--nexus.cs0.visual.force.com --- 5 parts, Instance is 2nd part
// (2) na12.salesforce.com --- 3 parts, Instance is 1st part
// (3) ion.my.salesforce.com --- 4 parts, Instance is not determinable
// Split up the hostname using the period as a delimiter
List<String> parts = System.URL.getSalesforceBaseUrl().getHost().replace('-api','').split('\\.');
if (parts.size() == 3) Instance = parts[0];
else if (parts.size() == 5) Instance = parts[1];
else Instance = null;
} return Instance;
} private set;
}
// And you can then get the Salesforce base URL like this:
public static String GetBaseUrlForInstance() {
return 'https://' + Instance + '.salesforce.com';
}
FYI: For Scenario (1), the 1st of the 4-part hostname can get really complicated, but you'll always be able to find the Instance name as the 2nd part. For those who are interested, the syntax of Scenario 1 follows this pattern:
<MyDomain>--<SandboxName>--<Namespace>.<Instance>.visual.force.com
Here you have a quite nice and small snippet, that does, what it should for VisualforcePages :-)
String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
// Example: c.cs7.visual.force.com
sUrlRewrite = 'https://'
+ sUrlRewrite.substring(2,6)
+ 'salesforce.com'
+ '/'
+ recordId;
// Returns: https://cs7.salesforce.com/00kM00000050jFMIAY
Use:    Url.getOrgDomainUrl().toExternalForm()
Thanks, Tim Lewis
Note behaviour changes between releases and is sensitive to My Domain settings:
#Future context returns https://na1.salesforce.com
Visualforce context returns https://na1.salesforce.com
Force.com Site context returns https://na1.salesforce.com
#Future context returns https://mydomain.my.salesforce.com
Visualforce context returns https://mydomain.my.salesforce.com
Force.com Site context returns https://mydomain.my.salesforce.com
My Domain is mandatory in new orgs effective Winter '21.
Enhanced Domains is mandatory in all orgs effective Summer '22.
// Not to be confused with Url.getSalesforceBaseUrl()
// http://na1.salesforce.com (can happen in async apex)
// https://c.na1.visual.force.com (local Visualforce Page)
// https://ns.na1.visual.force.com (packaged Visualforce Page)
// https://custom.my.salesforce.com (org has My Domain enabled)
// https://sandbox-mydomain.na1.force.com (sandbox site with My Domain...)
See also the Salesforce Identity API which attests the pod/instance endpoint.
Fix to Alex_E snippet:
String sUrlRewrite = System.URL.getSalesforceBaseUrl().getHost();
String sfBaseProtocol = System.URL.getSalesforceBaseUrl().getProtocol();
//remove namespace
integer firstDotPos = sUrlRewrite.indexOf('.');
sURlRewrite = sURlRewrite.substring(firstDotPos+1);
//replace visual.force with salesforce
sURlRewrite = sURlRewrite.replace('visual.force', 'salesforce');
sUrlRewrite = sfBaseProtocol+'://'+sURlRewrite;
serverURL = sUrlRewrite;
This works for me:
String sUrlRewrite = System.URL.getSalesforceBaseUrl().getProtocol()
+ '://' + System.URL.getSalesforceBaseUrl().getHost()
+ '/' + record.Id;
Here is something to do with regex
public String getURL() {
return String.format(
'https://{0}.salesforce.com',
new String[]{
URL.getSalesforceBaseUrl().getHost().substringAfter('.').substringBefore('.')
});
}

Resources