Google Drive API Authentication - google-drive-api

I'd like to make an application that could access MY own Google Drive anytime, create files there, share them and so on.
According to https://developers.google.com/drive/service-accounts "Use regular Google accounts as application-owned accounts" the only think I need is to get access_token and refresh_token once, store them in my application and using refresh_token I can refresh my access_token (somehow).
I can get access_token using request something like
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=http://localhost;response_type=token&client_id=
After approve this application request in user dialog I will be redirected to my localhost and I will get access_token that expires in 3600 seconds.
The questions are:
1. How to get a refresh_token?
2. How to refresh access_token using refresh_token?
I don't want to use Google's API client library because it's terrible (.NET).

Ok I got it.
The answer can be found here: https://developers.google.com/accounts/docs/OAuth2WebServer#offline
First You have to make an Auth request
<form method="POST" action="https://accounts.google.com/o/oauth2/auth">
<input type="hidden" name="scope" value="[YOUR SCOPE]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="response_type" value="code"/>
<input type="hidden" name="redirect_uri" value="[YOUR RETURN URL]"/>
<input type="hidden" name="access_type" value="offline"/>
<input type="submit"/>
</form>
Then you will get a 'code' to your return_url
Then you need to exchange the code to access_token and refresh_token
<form method="POST" action="https://accounts.google.com/o/oauth2/token">
<input type="text" name="code" value="[CODE YOU GOT IN PREV STEP]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="client_secret" value="YOUR CLIENT SECRET"/>
<input type="hidden" name="grant_type" value="authorization_code"/>
<input type="hidden" name="redirect_uri" value="YOUR REDIRECT URL"/>
<input type="submit"/>
</form>
As a result of this you will bet response like:
{
"access_token" : "[HERE YOU ACCESS TOKEN]",
"token_type" : "Bearer",
"expires_in" : 3600,
"id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6IjRURGtlQ0MzVWRPZHoyd2k1N2RnaUEiLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImlhdCI6MTM1MzQwNDQ3MCwiZXhwIjoxMzUzNDA4MzcwfQ.Va98sh9LvMEIWxpRMFkcuFqtDAUfJLN5M__oJyjvmIxQR9q2NUIoocyjqbNyXc7as_ePQYiUjajx0SCumtR4Zhv-exeJfrKA_uMmJTe7jWhK6K2R3JQ2-aIZNnehpEuhYZBXgLhzYz1mlFrLqQTdV6LjDhRPDH-ol4UKWXfbAVE",
"refresh_token" : "[HERE YOUR REFRESH TOKEN]"
}
Now you can store these tokens in your application and use for unlimited time refreshing the access_token every 3600 secs
<form method="POST" action="https://accounts.google.com/o/oauth2/token">
<input type="text" name="refresh_token" value="[YOUR REFRESH TOKEN]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="client_secret" value="[YOUR CLIENT SECRET]"/>
<input type="hidden" name="grant_type" value="refresh_token"/>
<input type="submit"/>
</form>
And each time you make this request you will get a new access_token
{
"access_token" : "[NEW ACCESS TOKEN]",
"token_type" : "Bearer",
"expires_in" : 3600,
"id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6ImpyYk5oNkRHZFN4Y0w5MUI5Q1hab2ciLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImNpZCI6IjI0Njg4OTY1NzQ4Ni5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImlhdCI6MTM1MzQwNTU5OSwiZXhwIjoxMzUzNDA5NDk5fQ.mGN3EYOX75gPubr3TqWIOBkfq-o3JBXMXx4MbxEBGMSuPdJi7VTqZa4isyR-st-J5_wTtA-j8tVQYnDeZDxj5KpJ14FFQPKTtv_VI5kvuT55KyOmGu4yidciYoffJMISisr8NqiksbemaiYX900sRv6PmoTA6Nf6VtHgj3BZjWo"
}

Related

To where does a form in an IFRAME POST?

I got this form inside an iframe and want to know where the post actually goes. When I go to the network tab, it only shows the src of the iframe and not where the POST request was sent.
<iframe id="popupOverlay_iframe" name="popupOverlayWindow" src="/cgi-bin/admin/radio_contact.cgi?action=add&member_org_id=46757"></iframe>
The page inside this iframe contains the following:
<form name="add_member" method="post" onsubmit="return submitForm(this);" _lpchecked="1">
<label>
<input type="text" name="email.email" id="email.email" value="" class="init_focus" placeholder="Email:">
</label>
<input type="hidden" name="action" id="action" value="add">
<input type="hidden" name="step" id="step" value="email">
<input type="hidden" name="type" id="type" value="employee">
<input type="hidden" name="member_org_id" id="member_org_id" value="46757">
<input type="submit" name="submit" id="submit" value="Next">
<input type="hidden" name="redirect" id="redirect" value="https://stagemms.nationalmediacalls.com/cgi-bin/admin/radio_detail.cgi?action=show&id=46757">
</form>
On submit, the JavaScript function submitForm is called and its result is returned.
If submitForm and thus the snippet returns false, the form won't be submitted. (The JS might do it's own request.)
If submitForm and thus the snippet returns true, the form will be posted to the current page (/cgi-bin/admin/radio_contact.cgi?action=add&member_org_id=46757) because no action was specified.
In this situation, you should have seen something like this in your network tab:
(/foo.html is url of the outside page, and /foo.cgi?action=add&member_org_id=46757 is the url of the page in the iframe.)

Correct way of post an object array using form post

I'm trying to use an payment service API in my application and following the documentation provided on the website. According to the docs, the POST body should look like this:
api_hash=38be425a-63d0-4c46-8733-3e9ff662d62d
&hash=ac0945d82b8589959b5f4ffafcc1a6c5983e82b8b4094c377a7b9c43d4a432bc
&order_id=2845
&amount=15
&currency=EUR
&email=stefan#my-test-store.com
&url_failure=http://my-test-store.com/order/fail
&url_ok=http://my-test-store.com/order/success
&items=[{"sku":"450","name":"Test Item","amount":"15","type":"item_type","qty":"1","price":15,"id":"5619","url":"http://example.com/products/item/example-item-name-5619"}]
I was able to successfully make a post request with postman however I'm confused about the "items" part in the post body since it's an object array. My html form looks like this:
<form method="post" action="#ViewBag.G2AConfig.PostUrl" id="g2apaymentform">
<!--PayPal Settings-->
<input type="hidden" name="api_hash" value="#ViewBag.G2AConfig.ApiHash" />
<input type="hidden" name="hash" value="#ViewBag.Hash" />
<input type="hidden" name="order_id" value="#ViewBag.OrderId" />
<input type="hidden" name="amount" value="#Model.Total" />
<input type="hidden" name="currency" value="USD" />
<input type="hidden" name="email" value="#ViewBag.G2AConfig.MerchantEmail" />
<input type="hidden" name="url_failure" value="#ViewBag.UrlFailure" />
<input type="hidden" name="url_ok" value="#ViewBag.G2AConfig.ReturnUrl" />
#foreach (var item in Model.Items)
{
<input type="hidden" name="items[#index][sku]" value="#item.Product.GameAccountId" />
<input type="hidden" name="items[#index][name]" value="#item.Product.Rank.Name" />
<input type="hidden" name="items[#index][amount]" value="#item.Product.MarketPrice" />
<input type="hidden" name="items[#index][qty]" value="1" />
index = index + 1;
}
</form>
I'm using Ajax post to make the request which looks like this:
event.preventDefault(); //prevent default action
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = $(this).serialize(); //Encode form elements for submission
$.ajax({
url: post_url,
type: request_method,
data: form_data
}).done(function (response) { //
$("#server-results").html(response);
});
This does not work and I get bad error response from the server. What's the correct way to submit a form with an object array?
Why do you create a form with the supposed objects before posting it?
If there is no real reason other than posting the values then i suggest you build the AJAX data via a javascript array or PlainObject as these are the other 2 possible data types for the data parameter. Currently you are using the serialized string.

How to generate a post action inside of an iframe for a payment gateway

I'm trying to set up an iframe-based payment gateway for a WooCommerce online store. The instructions I got from the bank say that I need to "generate a post action inside of the contained IFRAME on the page."
The result should be that when the checkout page loads, inside the iframe I should be seeing fields for the customer to enter their credit card information.
But I don't understand how to take the form below (from the documentation) and automatically post it to the iframe.
Payment Gateway Documentation:
To initiate a transaction, your system must generate a POST action inside of the contained IFRAME on the page.using the format below to the URL designated for your system. You will be provided with a URL for testing and live transaction processing by the processor when your merchant accounts have been configured.
Example:
<form ACTION="https://tranpage/demogwtxnif/txnif.aspx" METHOD="post">
<input type="hidden" NAME="MERCHKEY" VALUE="MYDEMOMERCHANTKEY" />
<input type=”hidden” NAME=”TRANTYPE” VALUE=”AUTH”/>
<input type=”hidden” NAME=”AMT” VALUE=”10.00” />
<input type=”hidden” NAME=”CURR” VALUE=”CAD” />
<input NAME=”invoice” VALUE=”DemoSale1” />
<input NAME=”tranid” VALUE=”123456789” />
<input NAME=”amt” VALUE=1.00 />
<input NAME=”curr” VALUE=”USD” />
<input NAME=”URLAPPROVED” VALUE=”http://www.webstore.com/cgishl/goodpayment.exe?res=#RC#&fres=#FC#&ac=#APP#&ref=#REF#&tran=#TRANID#&invoice=#INVOICE#&err=#EM#” />
<input NAME=”URLOTHER” VALUE=”http://www.webstore.com/cgishl/paymentincomplete.exe?res=#RC#&fres=#FC#&ac=#APP#&ref=#REF#&tran=#TRANID#&invoice=#INVOICE#&err=#EM#” />
<INPUT name=SUBMIT type=submit value="Pay Now">
</form>
Have the form's target attribute match the iframe's name attribute.
<form target="my-iframe" ACTION="https://tranpage/demogwtxnif/txnif.aspx" METHOD="post">
<input type="hidden" NAME="MERCHKEY" VALUE="MYDEMOMERCHANTKEY" />
<input type=”hidden” NAME=”TRANTYPE” VALUE=”AUTH”/>
...
</form>
<iframe name="my-iframe" src="https://tranpage/demogwtxnif/txnif.aspx"></iframe>
Source: https://css-tricks.com/snippets/html/post-data-to-an-iframe/
There is working example (you need to add a simple js line to submit your form):
var iframe = document.createElement('iframe');
var html = `<body>
<form id="form" action="http://example.com" method="post">
<input type="text" name="email" value="mail#example.com"/>
<input type="submit" />
</form>
<script>document.getElementById("form").submit();</script>
</body>`;
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
document.body.appendChild(iframe);

redirecting user to PayPal after submitting the data to db

i have a very frequent situation when submitting a form . I have a form that take order from user and after submitting the form it take user to PayPal for the payment . Issue is that how can i redirect the user to PayPal . I have tried almost all the possible solutions including $.post function of j query , JavaScript double action submission and php query string redirect but no one help me out . Its an humble request to all of you to help me out . just tell me the solution.
Additional : the form is custom and i have created my template to store the data in WordPress. The for is submitting to my database when i change the path of form action to my db path and when i change it to PayPal path its submit it to PayPal but not to my db.
A similar piece of code should work for you, subscriptionFrm is the ID of your form. In my case, index.php returns a JSON string with status about server-side processing (the JSON string is parsed with eval). Of course you need to append all PayPal hidden fields to your current form.
$(document).ready(function () {
$('#subscriptionFrm input[type="submit"]').click(function () {
$.post('index.php', $('#subscriptionFrm').serialize()).done(function (r) {
var result = eval('(' + r + ')');
/* Check result from your PHP code */
$('#subscriptionFrm').attr('action', 'https://www.paypal.com/cgi-bin/webscr');
$('#subscriptionFrm').submit();
});
return false; }); });
There is a very simple way to redirect to PayPal ,after processing the form's data. So you only should follow the principles in this entry.
First, I've set an action to the WordPress init, from my plugin's code:
add_action( 'init', 'my_init');
Now, it is time to implement the 'my_init' function:
if( !function_exists( 'my_init' ) ){ // Use function_exists to avoid conflicts
function my_init(){
if( $_POST[ 'unique_variable' ]){ // A form field to identify we are processing the form
//...process here your form
// and then print the form to be redirected to PayPal
?>
<form action="https://www.paypal.com/cgi-bin/webscr" name="myform" method="post">
<input type="hidden" name="business" value="seller#email.com" />
<input type="hidden" name="item_name" value="Product Name" />
<input type="hidden" name="item_number" value="Product Number" />
<input type="hidden" name="amount" value="10" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="lc" value="EN" />
<input type="hidden" name="return" value="URL to the product after check the payment" />
<input type="hidden" name="cancel_return" value="URL to use if user cancel the payment process" />
<input type="hidden" name="notify_url" value="URL of IPN in your website to check the payment" />
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="page_style" value="Primary" />
<input type="hidden" name="no_shipping" value="1" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
<input type="hidden" name="ipn_test" value="1" />
</form>
<script type="text/javascript">document.myform.submit();</script>
<?php
exit; // It is very important stop the WordPress in this point
}
}
}
You should modify the values of PayPal form fields, with your email, the URLs to your IPN, cancel and return page, the product's name, number and the amount to be charged.
Pay attention to the "exit" sentence after print the PayPal form, it is required to stop the PHP execution, the document.myform.submit(); submits the PayPal form after be loaded.
An excellent starting point in this subject is the premium version of plugin Calculated Fields Form (http://wordpress.org/plugins/calculated-fields-form/)
;-) Ericko

paypal express checkout landingpage billing issue

For some reason I can't get my paypal express checkout to allow guest-checkout without logging into paypal. I have the two vital form fields
<input type="hidden" name="SOLUTIONTYPE" value="Sole" />
<input type="hidden" name="LANDINGPAGE" value="Billing" />
but it still only shows login.
The full form looks like this (without products, normally there'd be item_1, item_2 and so forth):
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="myimg.png" name="submit" class="wp_cart_checkout_button" alt="Betal med PayPal - det er hurtigt, gratis og sikkert!"><input type="hidden" name="return" value="return.com">
<input type="hidden" name="business" value="my#email.dr">
<input type="hidden" name="currency_code" value="DKK">
<input type="hidden" name="lc" value="da_DK">
<input type="hidden" name="SOLUTIONTYPE" value="Sole">
<input type="hidden" name="LANDINGPAGE" value="Billing">
<input type="hidden" name="logoimg" value="logo.png">
<input type="hidden" name="PAYMENTACTION" value="Sale">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="rm" value="2">
</form>
If you're specifying this information in a , you're most likely not using Express Checkout.
Can you share the rest of your code? I have a strong suspicion you're using Payments Standard instead.
With Payments Standard you can't force the billing or login page; it's determined by cookie. If a PayPal login cookie is found, it'll display the login page. If no login cookie is found, it will display the billing page.