Facebook profile image is not displayed with <Image> - image

I got an app where I have to show an user profile image.
Some users are logged in by Facebook, so we are saving their Facebook profile image and when I try to render this image with React Native Image component, the image is not displayed.
What I got:
<Image style={ styles.userImage } source={{uri: "http://graph.facebook.com/{userID}/picture?type=small" }} />
And styles:
userInfoContainer: {
flex: 1,
alignItems: 'center',
paddingTop: 30,
paddingBottom: 30,
borderBottomWidth: 1,
borderBottomColor: '#e5e5e5',
backgroundColor: '#ffffff',
},
userImage: {
height: 100,
width: 100,
borderRadius: 50,
marginBottom: 20,
},
I don't know if the problem is that this Facebook image URL doesn't have image extension.
Any help is welcome.
Thanks :)

The issue is that the facebook graph url is not the actual url for where that image is stored. It is a redirect. There is an issue out on the react-native github which is tracking a similar issue (301 redirect). It looks like it is partially solved (working for 301's on iOS).
https://github.com/facebook/react-native/issues/4940
Some more details about this if you're curious:
Facebook gives you the graph url, but actually stores the image elsewhere on a cdn. This lets facebook move assets around without breaking the facebook graph url that they document. The graph url will redirect to the actual url, which means that the graph url returns a 302 (non-permanent redirect).
For example, currently my profile pic would be queried using this url:
https://graph.facebook.com/207537292916369/picture?type=large
But it will redirect (currently) to this actual location:
https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/15940479_407531822916914_4834858285678282755_n.jpg?oh=a49a86e0e8042e3df27ce3dfe871b958&oe=59327225

Looks like you have to remove one set of quotes from the uri string.

I think the problems lies in the picture url redirection. If you add &redirect=false at the end of your url and you use the url fields in the data object shown, the image should show properly.
I don't know the main reason for that but this workaround should help, even though it means writing more code.

It is possible and easy do this (get user profile's picture) using the Facebook React Native SDK.
1) Import the useful classes for this from react-native-fbsdk.
import { AccessToken, GraphRequest, GraphRequestManager } from 'react-native-fbsdk'
2) Implement the Facebook Graph Request in your method, like 'componentDidMount', as an example.
try {
const currentAccessToken = await AccessToken.getCurrentAccessToken()
const graphRequest = new GraphRequest('/me', {
accessToken: currentAccessToken.accessToken,
parameters: {
fields: {
string: 'picture.type(large)',
},
},
}, (error, result) => {
if (error) {
console.log(error)
} else {
this.setState({
picture: result.picture.data.url,
})
}
})
new GraphRequestManager().addRequest(graphRequest).start()
} catch (error) {
console.error(error)
}
3) Use the value of this.state.picture where you need.
<Image source={ { uri: this.state.picture } } />
I would recommend pass it by props, instead of handling as a stateful resource, but it will depend of your needs.

For those showing up to this thread years later like me, the thing that fixed it for me was just changing the Facebook Graph URL from http to https. React Native was able to display it properly after that.

Related

Quality option not working in cordova camera plugin

I have a mobile application that allows uploading images either via camera or choosing it from a photo library. While uploading the user is prompted if he or she wants a high quality image to be uploaded or not. If yes, quality option is set to 100 else set to 50. This seems to work fine for android and ios builds but not for windows build. Even after selecting the high quality option, the image uploaded has low quality.Is there something that I'm missing? A plugin that might need to be added specific to windows to make stuff work?
Please help.
I've included a part of the code snippet:
.factory('$Camera', function ($q, $crypt, $settings) {
return {
getPicture: function () {
var q = $q.defer();
var options = {
quality: $settings.getValue('uploadHighQualityImage') ? 100 : 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.CAMERA,
targetWidth: 500,
targetHeight: 500
};
navigator.camera.getPicture(function (result) {
// Put the options
q.resolve(result);
}, function (err) {
q.reject(err);
}, options);
return q.promise;
},

logged in github session in electron

Is there any way i could inject a logged in github session in a electron window?
let win = new BrowserWindow({
width: 800,
height: 600,
})
win.loadURL('https://www.github.com')
Electron has a Cookies API that you can use to do just this. So for example you would have something like the following, and just set all the cookies you need to. (example taken from documentation)
const cookie = {
url: 'http://www.github.com',
name: 'dummy_name',
value: 'dummy'
}
session.defaultSession.cookies.set(cookie, (error) => {
if (error) console.error(error)
})
Also, when writing a feature such as this make sure you are being secure and responsible. Ensure this does not open a vulnerability for users and that user privacy is being respected.

Fine-Uploader - retry with "scaling" and "hideScaled" only sends original image

I'm calling Fine-Uploader to upload images with client-side scaling, and I'm hiding the scaled images. If there's a bug in my server-side receiver CGI script then all uploads fail and the image shows a "Retry" button, but clicking it (after fixing the bug!) only causes the failed original image to be resent. The scaled images are lost. Here's the fine-uploader call. What am I missing to get it to resend the scaled images if they failed?
$('#fine-uploader-manual-trigger').fineUploader({
template: 'qq-template-manual-trigger',
request: {
endpoint: 'cgi/fine-uploader.cgi',
},
thumbnails: {
placeholders: {
waitingPath: PLACEHOLDERS+'waiting-generic.png',
notAvailablePath: PLACEHOLDERS+'not_available-generic.png'
}
},
// request client-size scaling - causes upload of "image (size).jpg" in addition to "image.jpg"
scaling: {
sizes: [
{ name: "thumb", maxSize: 200 },
{ name: "popup", maxSize: 600 },
],
hideScaled: true
},
autoUpload: false
});
$('#trigger-upload').click(function() {
$('#fine-uploader-manual-trigger').fineUploader('uploadStoredFiles');
});
};
From the scaled images documentation page:
Since they will not be represented in the UI, this means that they cannot be deleted, canceled, or manually retried via Fine Uploader's default UI. Failures of these scaled versions will obviously not be apparent to users by default if you elect to hide them from the UI.
If you want to manually retry hidden scaled images, you'll need to get a handle on their IDs and use the retry API method.

Error uploading photo to Appcelerator ACS using Trigger.io

I'm trying to upload a photo to Appcelerator Cloud Services (ACS) storage using Trigger.io.
I can't figure out the correct syntax to use for the file object.
I'm getting an error "Error: Syntax error, unrecognized expression: #[object Object]"
Here's my relevant code:
$("#photograph-record").on("click", function(){
forge.file.getImage({source:"camera", width: 280, height: 280},function(file) {
var data = {
photo: file //the ID of file input control
};
sdk.sendRequest('photos/create.json', 'POST', data, callback);
});
});
Here's the Docs for the ACS Photo class - http://cloud.appcelerator.com/docs/api/v1/photos/create
Required Parameters - photo: the attached binary file
Since it needs to be a binary I tried "photo: forge.file.string(file)" (http://docs.trigger.io/en/v1.4/modules/file.html#modules-file), but got an error on the Appcelerator side "Photo parameter required for photo upload".
I have no problem passing the image into my App page views using forge.file.url, so I know there's no problems with the file object, it's just figuring out the correct syntax to pass it as a binary to the sdk.sendRequest call.
Any ideas on what I need to be passing in the data variable to get this to work?
The Appcelerator docs are pretty good here - it looks like they're expecting a POST parameter called photo which contains the binary image data.
To do that using our request module:
$("#photograph-record").on("click", function(){
forge.file.getImage({source:"camera", width: 280, height: 280},function(file) {
file.name = 'photo'; // the magic
forge.request.ajax({
url: 'https://api.cloud.appcelerator.com/v1/photos/create.json',
files: [file],
success: function () { ... },
error: function () { ... }
});
});
});
I don't see a way to use their JS library here, because they're expecting you to pass in the id of a HTML form element to get data from, but we're interacting with the camera or gallery directly...

Extjs 4 (with a code for 3.4 below) downloading a file returned from a post request

I have seen questions slightly related to this, but none that answer my problem. I have set up an Ext.Ajax.request as follows:
var paramsStringVar = 'param1=1&param2=two&param3=something&param4=etc';
Ext.Ajax.request({
url: '/cgi-bin/url.pl',
method:'POST',
params:paramsStringVar,
timeout:120000,
success: function(response, opts){
var objhtml = response.responseText; //content returned from server side
console.log(objhtml);
}
});
This request retrieves the appropriate content from the backend. One parameter is outputType, which can take values {html, excel, csv}. When returning html to display I am able to handle and display it correctly. Now on to the problem...
When I set the outputType parameter to csv or excel, I get back the appropriate content as csv or tsv(excel) as requested. BUT, I don't want the content, I want a prompt to download the file(csv or excel). How can I have the browser auto prompt the user to download the file instead of just retrieving the text content within extjs?
Version 4.07 so I can't use any 4.1 only features
There seems to be no bulletproof solution but there are several approaches I would try:
1) Use an iframe instead of real XHR to POST data to the server, e.g. <form action="/something" target="myiframe"> where myiframe is the name of your hidden iframe. That way your form would use the iframe (not your main window) to submit data to the configured URL. Your server should set response header as application/octet-stream (or some ither MIME type for binary data) so the browser triggers download. Otherwise (if html returned in your case) you can just retrieve iframe's body innerHTML and display it to the user in UI. While using an iframe (or a new window) instead of XHR doesn't sound like the best idea, this solution seems to be the most reliable so far (and with best browser support).
Here is a slightly modified example from Ext.form.Basic docs page:
Ext.create('Ext.form.Panel', {
title: 'Basic Form',
renderTo: Ext.getBody(),
width: 350,
// Any configuration items here will be automatically passed along to
// the Ext.form.Basic instance when it gets created.
// *THIS* makes the form use a standard submit mechanism, not XHR
/**/standardSubmit: true,
// URL to submit to
url: 'save-form.php',
items: [{
fieldLabel: 'Field',
xtype: 'textfield',
name: 'theField'
}],
buttons: [{
text: 'Submit',
handler: function() {
// The getForm() method returns the Ext.form.Basic instance:
var form = this.up('form').getForm();
if (form.isValid()) {
// Submit the Ajax request and handle the response
form.submit({
success: function(form, action) {
Ext.Msg.alert('Success', action.result.msg);
},
failure: function(form, action) {
Ext.Msg.alert('Failed', action.result.msg);
},
// You can put the name of your iframe here instead of _blank
// this parameter makes its way to Ext.form.Basic.doAction()
// and further leads to creation of StandardSubmit action instance
/**/ target: '_blank'
});
}
}
}]
});
There are two key parameters here (lines marked with /**/):
standardSubmit: true config that you pass to your form will make it do a standard submit instead of XHR.
Passing a target parameter to the form's submit action. This feature is not documented but you can see it being used in Ext.form.action.Submit source code (all options that you pass to Ext.form.Basic.submit() method end up as parameters of Ext.form.action.* instance.
In the example code I put target: '_blank' to demonstrate that it works right away (will create a new browser window). You can replace it with the name of your iframe later but I suggest that you first test how your form submits data to a regular new window and then develop logic that creates and processes an iframe. You will have to process the result inside iframe yourself, thought. It's not that difficult, see Ext.data.Connection.upload() implementation as an example of iframe processing.
ExtJS actually already uses the iframe technique for file uploads. See Ext.data.Connection and Ext.form.field.Field.isFileUpload() for an idea of how it can work.
2) Suggested here: Using HTML5/Javascript to generate and save a file.
If you don't want to go the iframe way, you can try generate data URI from response data and navigate to that URI triggering download:
content = "Hello world!";
uriContent = "data:application/octet-stream," + encodeURIComponent(content);
window.location.href = uriContent;
Again, mimetype is essential here. This worked for me, you should note, however, that browsers impose a size limit to data URIs (256Kb is a safe bet).
3) Another answer in the mentioned thread links to FileSaver.js library the implements the (abandoned?) w3 spec. Usage and demo here. It uses [BlobBuilder] to generate a blob of binary data that is further used to initialize downloads using one of several methods. While this solution seems to work, it uses deprecated APIs and may not be future-proof.
Below is my solution. This is how I have it currently working. The response generates a download/open prompt, based on a response type of text/csv. Note that no iFrame or reference to an iframe are needed. I spent a lot of time hung up on the need for an iFrame, which actually broke my solution. An iFrame is not needed to generate a download prompt. What is needed is a request(submittal) similar to this one, along with a backend generating the appropriate csv with text/csv response header.
var hiddenForm = Ext.create('Ext.form.Panel', {
title:'hiddenForm',
standardSubmit: true,
url: /cgi-bin/url.pl
timeout: 120000,
height:0,
width: 0,
hidden:true,
items:[
{xtype:'hiddenField', name:'field1', value:'field1Value'},
// additional fields
]
})
hiddenForm.getForm().submit()
The standardSubmit line is vital
You don't need to create a form panel and make it hidden in your extjs file. We can add a html form and on click of button in extjs file we can submit the form using the url. This will work both in IE as well as chrome browsers. Below is my code i tried and its working fine,
<form action="<%=fullURL%>/DownloadServlet.do" method="get" id="downloadForm" name="downloadForm" target="_self">
</form>
click:
{
fn: function()
{
document.getElementById('downloadForm').submit();
}
}
To get it working on ExtJS 3.4:
var hiddenForm = new Ext.FormPanel({
id:'hiddenForm',
region: 'south',
method: 'POST',
url: "/cgi/test.wsgi",
height: 0,
standardSubmit: true,
hidden:true,
items:[
{xtype:'hidden', name:'p', value:p},
{xtype:'hidden', name:'g', value:g},
// ...
],
});
linkThis = new Ext.Button({
text: 'Download this CSV',
handler: function() {
hiddenForm.getForm().submit();
},
maxHeight: 30,
});
Remember that in order to make it working, you should put the hiddenForm in any container (i.e. in the same Ext.Window of the button), for example:
risultatiWindow = new Ext.Window({
title: 'CSV Export',
height: 400,
width: 500,
....
items: [...., hiddenForm]
});

Resources