Create Docs through the Drive API with specific page setups (margins, orientation) - google-api

My app is already able to create Google Docs through the Drive API, and when doing so I can choose the title, data, and metadata. But I've been looking at the documentation and it seems there is no way to set some other properties of the Docs, such as the margins and the orientation (portrait or landscape) of the Docs being created. Is there a way to do so?
After not finding an answer in the documentation, the only thing I could think of was logging in with my Google Account, going to one of the created Docs, click on "Page setup", then select "Landscape", and finally "Set as default". I thought that by setting it as the default in my Google Account, it may happen that all the Docs created with it may be created as Landscapes. But it didn't work. A possible reason, however, is that the account actually creating the documents is a Service Account, not my regular Google Account. I guess both accounts, while being linked, are not the same thing, so the defaults of one don't apply to the other. Is there some way to set "Portrait" as the default orientation for the Docs created with my Service Account? If not, would the solution be to stop creating the Docs with my Service Account, and start creating them with my regular Google Account, so that the defaults I apply to them apply too to the newly created Docs?

Drive API doesn't support Docs related features for you to retrieve/set metadata about the documents.
Service accounts are individual Google accounts and no way related with your own user account. On the other hand, your default settings will only apply to you. You cant programatically set other user's defaults.

The best way I can think of, which is really a hack, is to make your own google docs API using headless chrome and pupeteer, which could be used only in part, and in connection with, the official docs API.
This is basically a web browser that runs on your server, which can be controlled to do anything a client would normally do on a web browser, like click buttons, submit forms, etc, only at the command of a server, which can be commanded through HTTP requests etc.
So after installing puppeteer, open the chromium browser, log into your google account, then open google drive with the server, and you should already be logged into the account. If not, you may have to log into the account using the server logic, which can get complicated with 2 step verification...
but anyway once you are logged into your google account on puppeteer, on drive.google.com, simply stay on that page and wait.
If the server receives some kind of HTTP request to make a new document, then have the server just literally click the new document button on the webpage, and it should open the new document in a new tab, which you should be able to access with puppeteer.
Then, when it is made, you can either just send the URL back to the request server, and / or make your page setup changes now, based on the data received in the HTTP request earlier.
For example, if, at one point (either after the document is created, or during its creation), there is some kind of message specifying the color of the background, just manually, on the server side, click File -> page setup, and select Custom, and, programmatically "type" the hex color code etc then click the OK button, then end the POST request.
If you have any questions, let me know

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
//turn landscape if portrait
var oldHeight = body.getPageHeight();
if (oldHeight > body.getPageWidth()){
body.setPageHeight(body.getPageWidth());
body.setPageWidth(oldHeight);
}

Related

MS Teams custom tab app changes Session.SessionID between requests

I'm making a custom tab app of MS Teams with ASP.NET, however, the tab doesn't seem to pass a same cookie between requests on MS Teams. So the ASP.NET app behind the tab generates a new Session.SessionID on every request.
I've checked the following question, and tried some settings according to that page, but nothing helped me. Actually my web site works nicely if I navigate it via Chrome or Edge.
ASP.NET: Session.SessionID changes between requests
How do I get a same cookie between requests on MS Teams?
I've not tested this specifically so haven't seen it, but basically the broad idea of session is to have to uniquely "remember" a user, and then restore State for them from a location (e.g. database). From your question, it seems like the out of box "Session" object is giving trouble, but at any rate you should probably avoid using it because it won't "remember" the user even across devices.
However, Teams provides you a way to achieve the same thing yourself quite easily. Remember that the Teams 'Context' object provides a userObjectId property that is unique and valid for the same user on all sessions on all devices (it's actually their Azure Active Directory id). You can simply store whatever you want in your own database, key'ed by this id, and request it on page load. It's also possible to get this from the querystring for a static (personal) tab if you want to handle the behaviour server-side (e.g. C#).

How can I build a webapp which uses google calendar api without having to become verified?

What I want to build:
I want to build a website where users can connect their google calendars (this will use Google Calendar API's)
and view their calendar events, as well as edit them, and create new ones.
My problem:
In order to do so, google says my app needs to be verified, which can take weeks, and I also need to set up terms of services pages, privacy policy pages
I also need to supply authorised javascript origins which MUST start with https, which of course is a problem during development, since my origin is http://localhost
I also need to set up support emails and homepage link
Question
I just want to start building my application without having to set up a whole production-ready website eco system.
Is there anyway I can use these Google Calendar APIs for editing/creating calendar events locally, without having to set up everything mentioned above first?
Unverified apps can still be used by the developer who created the project on google developer console.
Unverified app screen
The app or script might display an "unverified app" screen before it displays the consent screen. This is based on the specific scopes that your app includes in the request.
You can still work on your app while you are going though the verification process. However that being said i would start that process asap it can take a long time to get verified.
Yes, you can. As far as I am able to tell, all the verification step does is remove the "unverified app" screen. As long as you click Advanced > Go To ... (unsafe), you should be able to create and edit calendar events for that user in your application.
In order to be able to create and edit calendar events, you need to use the most sensitive scope, which is https://www.googleapis.com/auth/calendar. I couldn't figure out how to edit and create calendar events in my web app until I changed my scope from calendar.events to calendar.
Creating Events: https://developers.google.com/calendar/create-events

Automating Wi-Fi Authorization with Firefox SDK

I'm trying to make a Firefox addon with the Firefox Add-on SDK and API, and I have some questions about their possibilities before I start using them.
My college's Wi-Fi authorization expires every 30 minutes only to have fun pissing off their students. There are already some autofill addons available on many browsers but it's still destructing to move the mouse pointer onto the "Login" button and click it when there is one second left to turn in a midterm paper. I've heard my friends complaining like this for months and I myself think the thing is actually annoying sometimes, so I decided to develop a Firefox addon that takes charge of the job so that the authorization process will feel not even existing once the addon is activated. (I just want to impress my friends honestly.)
For ease I would like to develop the addon within the Firefox add-on SDK. I found that my addon would be utilizing the page-mod, password and request APIs; page-mod to detect the Wi-Fi service's auto-redirection into their authorization page, password to fill in the page's form by a student ID and password stored in the individual Firefox browser, request to redirect the "Login Successful!" page into the originally given destination.
So I guess it should be possible to achieve my goal with this SDK and APIs, but there are still some questions that I need to ask before I proceed:
Is it possible to pass a callback function to page-mod::PageMod (not as a String or a URL to another JavaScript file)? If not, can it be done using the lower level API?
Is it possible to actually redirect a page in a tab into another page only using high level APIs?
Is it possible to remember the original destination's location (with the request method and contents) and call it in the process of page-mod::PageMod (in order to re-redirect out of the authorization page)? If not, can it be done using the lower level API?
Is it possible to perform the addon's redirection function on inactive (background) tabs where the opened webpages automatically keep connecting to the Internet and get redirected to the authorization page?
Thank you so much for reading and please spare a little bit of your time for me. Thank you again!
Is it possible to pass a callback function to page-mod::PageMod (not as a String or a URL to another JavaScript file)? If not, can it be done using the lower level API?
No, everything that goes through the port is serialized using JSON serialization (See docs). Instead you would probably emit an event from your content script to execute the callback method with parameters you pass it in the module scope and hardcode parts that need to be done in the content script with port event listeners.
Is it possible to actually redirect a page in a tab into another page only using high level APIs?
Totally, if you're in a content script, you can just set window.location, or in your modules you set the location of a tab, see https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#url.
Is it possible to remember the original destination's location (with the request method and contents) and call it in the process of page-mod::PageMod (in order to re-redirect out of the authorization page)? If not, can it be done using the lower level API?
Possible? Yes, depends a lot on how the redirection from the W-LAN works. Generally all the SDK offers you is getting load/ready events for tabs and reading a tab's current URL. So if you get a ready event at the point of the redirect you're fine. If your college login remembers the redirect target using a get parameter in the URL you're fine. If your college doesn't adjust the URL, you're fine. If you really need to dig through the request, you'll have to ge a bit deeper than even what the SDK offers you, but it is possible.
Is it possible to perform the addon's redirection function on inactive (background) tabs where the opened webpages automatically keep connecting to the Internet and get redirected to the authorization page?
JS execution for Add-ons is not paused based on a tabs state.

How do I register a Google cloud application?

In the Google plus Quick Start documentation:
Google Plus Quick Start Enable Google plus API
It states that a form needs to be filled out to register the app. But I can't find anything to click on to register the app. I'm wondering if the Google Console layout has changed, and the documentation hasn't been updated? I did a search on the word, "register" but didn't find anything.
The side bar on the left that I see has a list like this:
Overview
----------
APIs and auth
Credentials
Consent Screen
Push
That's what my Google Console screen looks like. I don't see any menu item for Register App
True, that part changed a bit. What you need to do now is to go to "Credentials" and create a new Client ID there. You will then find the necessary Client ID and Client secret listed on that page.

Google API Key for translation

what key (if any) do I require for simple text translation from program?
Note: I don't host any website.
Getting a Google Translate API Key
To use the Google API, you first need a Google Cloud account.
Go to https://cloud.google.com/translate/docs/basic/setup-basic
Click Set up a project.
Name the new project in the Enable Cloud Translation API screen. Use this dialog to name your project
They may ask you to create or connect to an existing billing account. Google gives you a $300 credit to use the Cloud Translation API over a year to try it out.
Create a new service account.
This screen will pop up with your new project name and the associated service account. Click Download Private Key. This API key (written in JSON) connects your site to the Google Cloud. To enable API, download the private key.
Upload the Google Translate API key to your site. Check with your hosting company where on your system to place this key.
When you’ve placed the Google API key on your site, tell your system where to find it. Set an environment variable. Again, check with your hosting company if you don’t know how to open a terminal.
API keys are the same for all GCP APIs. Instructions here: https://cloud.google.com/docs/authentication/api-keys
Activate the Google Translate API
Before you can use a Google API in your project, you have to activate it. Go to the side menu and select the APIs & Services option:
Now you will see a screen with statistics about the APIs that you have activated. If you created the project from scratch by following the steps above, you won’t have any data yet, as you can see:
Click on the upper button Enable APIs and services to continue with the activation process of the API. This takes us to a search box where we have to look for the API we’re interested in. In this case, we want to use the Google Translate API. Type translate in the search box and click on the result Cloud Translate API:
This brings us to a screen with the description of the Cloud Translation API. Click on the Enable button to activate the API in our project:
We already have the Google Cloud Translation API activated. We’re almost there…
Create a new API Key for Google Translate
After activating the API, let’s see how to generate a new Key API to be able to use this service. We have to go to the side menu again and select the Credentials option:
On this screen we see a button with a drop-down and the text Create credentials. Don’t click on the button! Instead, open the drop-down by clicking on the arrow to the right of the button and select the API Key option.
This creates the new Key API. You can copy it if you want, although you can access it later:
Google Cloud provides us with a new Key API to use Google Translate with our third-party applications.
How to Restrict Our API Key to Protect and Limit Its Use
To control the cost of Google Cloud by using the Google Cloud Translation API (or Google Translate, which is the same), we can do two things: restrict where you can use the API Key that we just created or limit the allowed quota to use the service itself.
In the screenshot above, if you click on the Restrict Key button you will go to the API Key restriction screen that follows:
There you can select to restrict the API Key by HTTP referrers, which means that you can only make calls to the Google Cloud Translate API using the API Key from certain domain names.
You must add the valid domain names in the text box that appears when selecting the HTTP referrers option. Sample domain name https://google.com/*.
On the other hand, go to the menu API’s & Services → Dashboard → Cloud Translation API → Quotas and there you will find a box called Characters. There you can modify the quota limits of the Google Cloud Translation API and reduce them, if necessary.
The price for Google Cloud Translation API at the time of writing this post is 20 USD per million translated characters, so make your numbers.
And that’s all! You already have your API Key ready to be used with the application you want. The process is a bit cumbersome at first, but following the steps you’ll get the API Key without problems.
An API key is required to use the Google Translate API and you can get yours from the Google APIs Console.
For further details, check the Developer's Guide.

Resources