I have created an app with react native, where the user is able to log in with facebook. I request the users first_name, last_name and picture.width(520)(which is the users profile picture).
When the user log ins for the first time it works fine. In the profile section you can actually see the picture, however after a day or so if you log in again the profile picture wont load. In the database I can still see the same URL given to me by the request, it just wont load.
Does FB change the URL or how can I permanently get a URL that points to the users profile picture?
The actual request:
const infoRequest = new GraphRequest('/me',
{
parameters: {
fields: {
string: 'first_name,last_name,picture.width(520)'
}
}
}, (error, result) => {
//save picture URL to database
})
Related
I am using stripe with django and I want to pass some information I received from the checkout session to success page.
After reading the documentation https://stripe.com/docs/payments/checkout/custom-success-page#modify-success-url I modified success url to
MY_DOMAIN = 'http://127.0.0.1:8000/orders'
success_url=MY_DOMAIN + '/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url=YOUR_DOMAIN + '/cancel/',
metadata={
"price_id": price_id,
}
)
def Success(request):
session = stripe.checkout.Session.retrieve('session_id')
return render(request, 'orders/success.html')
But this gives me an error:
Invalid checkout.session id: session_id
If I manually put instead of "session_id" the actual id that is printed in the url everything works fine.
So my question is what should I write instead of 'session_id' in order to retrieve the session?
When using the prebuilt checkout page, you provide Stripe with the success url to send the user to when they successfully complete a payment. This is done like so:
checkout_session = stripe.checkout.Session.create(
line_items=[{'price': price, 'quantity': 1}],
payment_method_types=['card'],
mode='payment',
success_url="http://yoursite.com/order/success?session_id={CHECKOUT_SESSION_ID}"
cancel_url=domain + cancelURL,
)
return redirect(checkout_session.url)
This will create a Stripe checkout session and send the user to it.
Once the user successfully pays, Stripe will send them to the success_url you provided and put in the correct checkout session id.
You will have to use webhooks to get the data you want, save it to your database, and then query the database in the success page view. Getting the checkout session id from the success page URL will depend on if you are using a GET like in the example above or as part of the URL like https://yoursite.com/order/12345. If you use the GET method, see here for more info.
I have a django app where users can save their contacts. I am not building a flow to allow users to download their contacts locally.
To do so, I built a CTA ("download") that if clicked
Gets the id of the contact selected
Triggers an Ajax request to my views
In the view I get the contact id, retrieve the data from my DB and create a VCF card out of it. (A contact card - basically a text file)
Now I would like to have such file downloaded on the client's machine but I don't know how to do it.
I managed to do it if I redirect to a new url where the view does exactly what my view below does, I want to allow users to download the file without being redirected to a new page. Also, trying to avoid storing the contact ids in the URL.
That's why I am trying to use ajax but I think that's creating problems because and Ajax request waits JsonReponse from the view.
I tried both a GET or POST but it's not working.
This is my view currently:
def get(self, request, *args, **kwargs):
#Get the ids
ids = request.GET.getlist('contact_ids[]')
# Get contqacts associated with ids
contacts = Contact.objects.filter(id__in=ids)
# Transform contacts into long text
text = Vcard().GroupVcards(contacts)
#Create file on the fly and attach it to the response (is this correct actually?)
response = HttpResponse(content_type='text/plain')
response['Content-Disposition'] = 'attachment;filename=ven.vcf'
response.writelines(text)
#return
return response
This is the Jquery triggering the ajax
$('.Vcard').on('click', function(e) {
let id = $(this).data('contact_id')
$.ajax({
type: "GET",
url: Urls['action:DownloadContact'](),
data: {
csrfmiddlewaretoken: csrftoken,
'contact_ids': [id],
},
error: function(response){
console.log(response)
console.log('error')
},
success: function(response) {
console.log(response)
console.log('success')
}
});
})
I'm trying to integrate Stripe Checkout to have the customer pay when submitting the form. This integration redirects the customer to Stripe's page to do the payment. How can I make Django remember the input values from the Form after the payment is submitted on the redirected page? Can I create a custom URL that remembers the values?
Also their script requires a payment ID which I serve through a variable but this variable is first created once the form is submitted. I can't change a javascript variable dynamically once the page is rendered, right?
Javascript
<script>
stripe.redirectToCheckout({
sessionId: "{{context}}",
}).then(function (result) {
// Diplay result.error.message to your customer
});
</script>
Python code to create the payment which is called after form submission
stripeUID = str(uuid.uuid4())
payment = stripe.checkout.Session.create(
success_url="https://mypage.com/succes",
cancel_url="https://mypage.com/error",
payment_method_types=["card"],
client_reference_id= stripeUID,
line_items=[
{
"amount": 2000242,
"quantity": 1,
"name": "Blender rendering",
"currency": "usd",
}
]
)
context = payment.id
For the payment ID, use AJAX. Submit the form with Javascript, and in the AJAX response serve the payment ID. then you can use it in the redirect. As for the redirect, you will need to create a model to store the form value against stripe session ID, so when your customer gets redirect back to your website, u can query the table with the session ID to get their original form values.
AJAX calls can be done by using fetch, XMLHttpRequest, jQuery.ajax
I'm working with a static webpage hosted on S3 and I'm using a Django as my api for session management.
I have my JS code on S3 POSTing to django when users sign in to my web site but I'm not getting any cookies saved on my browser.
I'm not sure if it's the jQuery $.post that is not accepting cookies or if it's Django that's not allowing the sending of cookies. How would I overcome this?
I currently also have a url endpoint on my django app where I can check if cookies are working but when I hit the url I get the standard message
Please enable cookies and try again.
Although my browser accepts cookies.
I get these urls to work fine when I use a Django rendered page to interact with them.
Any help would be greatly appreciated - thanks
Edit - showing code:
I use Janrain to allow user to login via facebook, twitter, etc..
how Janrain works
user clicks sign in button, uses facebook/wtv to login
Janrain get's user data on their servers and gives you a token which I post to django via this function
janrain.events.onProviderLoginToken.addHandler(function(tokenResponse) {
$.ajax({
type: "POST",
url: post_form_url,
data: formdata + "&token=" + tokenResponse.token,
success: function(res, textStatus, jqXHR) {
//do stuff now that we are logged in ...
console.log(jqXHR.getResponseHeader('Set-Cookie')); //returns null
//$('#redirect').submit();
},
});
In Django
def login_via_janrain(request):
if request.method == "POST":
janrain_token = request.POST.get('token')
// communicate w/janrain api to get info from this token (i.e. who just logged in via this token)
#create (or pull) user based on data returned from janrain api and do a Django login
auth.login(request, user)
#return success msg to $.post (session cookies should be automatically included in the response)
return HttpResponse("success")
Using S3
I currently have a static site hosted on S3. I tested these urls using django rendered views but ultimately I want the S3 site to render the views and just use django as an api endpoint.
I can currently login from the S3 site but after a successful login I have no cookies to show for it... I think django isn't sending back cookies...
I simply don't know how to do it.
I have an html linked with a knew url, with two buttons.
imagine that url is only shared with two persons/users.
each person have to push the button signed their name.
I tried using request, but I couldn't figure out how to know if more than one user is active in that view.
The another point is:
One person reach the url first, how to make an "refresh" in their browser, when the second one appear?
You'd have to store who's at what view in some model.
class UsersInURL(models.Model):
users = models.ManyToManyField(User)
url = models.CharField()
# you'd probably want to use a through table and set up some expiration logic
Then remember to modify this model every time a logged in user views a page; middleware sounds perfect for this.
class UserTrackerMiddleware(object):
def process_view(self, request, *args, **kwargs):
UsersInURL.users.through.filter(user=request.user).delete()
users_in_url, created = UsersInURL.objects.get_or_create(url=request.path)
users_in_url.users.add(request.user)
Then to refresh their page, you'd need to set some kind of communication between the server and the browser which pings this model and refreshes if it detects some change.
var lastCheckedUsers = undefined;
function checkForNewUsers() {
$.get('/some-ajaxy-url/', { url: document.URL }, success: function(data) {
// you'd have to set up a view that returns active users # url
if (lastCheckedUsers != data.users && lastCheckedUsers != undefined) {
window.location.href = window.location.href;
};
lastCheckedUsers = data.users; // store last state; refresh if modified.
})
};
setInterval(checkForNewUsers, 1000);
I'm sure that should get some ideas flowing.