How to fix Google tokens Expiring after seven days - google-sheets

Is there anyway to get a permanent oauth2 token for Google Sheet API or a semi permanent ?
The one I'm using right now expires once every 7 days which is not very helpful.
If not, is there any alternative for an online spreadsheet application that you can connect with python ?

This depends upon what you are doing if you are accessing a sheet that you own personally then you could use a service account. Service accounts are preauthorized and will not expire.
If you are access sheets that are owned by your users then you will need to use Oauth2. If you request off line access then you will get what is called a refresh token. Refresh tokens can be used to request new access tokens when the access token expires.
As you say that the token is expire after seven days implies that you are using an a refresh token currently. However you have not set your project in google cloud platform to production. As it is still in testing your refreshtoken will expire after seven days.
A Google Cloud Platform project with an OAuth consent screen configured for an external user type and a publishing status of "Testing" is issued a refresh token expiring in 7 days.
How to switch to production.
Go to Google cloud console for your project
Find the Oauth consent screen on the left.
Click got to production

Related

Google oauthplayground make refresh token not expire

I have developed an express app that's purpose is to send emails. I authorized an email sender using googles oauth2 playground, and their google api. I wanted to use this strategy rather than working with a third party email sender to not be subject to vendor lock-in. I had the system working successfully, but after a day it stopped working with "invalid-grant". In production, I'd like to have a permanent email address (under their google workspace) that is solely dedicated to these bot emails. The oauthplayground says you can avoid the 24 hour expiration, but I recall doing that before and it eventually came up with invalid grant. Is there a better way to set up a bot like this? Or should I just try the oauth playground and using the config panel to avoid the refresh token expiration again?
To start with let me say that invalid-grant means that your refresh token is no longer valid. It is no longer valid because googles oauth2 playground is intended for use for testing purposes only.
Access tokens will expire in less than an hour and refresh tokens created using it will expire in 24 hours.
While it is possible to configure it to uses your personal client id and client secret it is still not the optimal way to go
Even using this method for production is not going to work well for you. If your app is still in testing phase then your refresh token is goin to expire in seven days. You will need to set your app to production in order to have a refresh token that does not expire.
The issue then becomes that in order to use the gmail api your going to need to have your application verified. You can not verify your application using the googles oauth2 playground as a redirect uri as you can not verify that you own this domain.
The solution to your problem is to create an application of your own hosted on your domain. This will allow you to create the credentials you need and have your app verified by google.
under their google workspace
You made one comment that is not clear. If you are using google workspace then why not just set up a service account with domain wide delegation to the user on the domain. You will avoid all the issues you are having above.

Managing Google Sheets API Refresh Token

I am using Google Sheets for my website to store certain order information. The problem I'm dealing with is that the access_token expires within about an hour and the refresh_token lasts me a couple of days at most before it expires.
Once my refresh_token expires I have to:
Physically log into Google with my app URL
Accept Scopes and copy the auth code
Make a code grant request to get issued a new refresh token
The above process is manual, tedious and unbearable after a month.
Is there a better way?
The problem I'm dealing with is that the access_token expires within about an hour and the refresh_token lasts me a couple of days at most before it expires.
Refresh tokens only last seven days if your application is still in testing. Go to Google cloud console and set it to production. Your refresh token will then stop expiring.
I am using Google Sheets for my website to store certain order information.
If you are only using a sheet that you the developer control. This meaning that you are not accessing sheets owned by your users on their own google drive accounts. Then you should consider using a Service account. Should you be using a Google service account
Unfortunately, I cannot understand your actual situation and your script, how about the following pattens?
Pattern 1:
In this pattern, the service account is used. When the service account is used, the access token can be retrieved without authorizing the scopes using the browser. You can use Sheets API using the access token.
Pattern 2:
In this pattern, Web Apps created by Google Apps Script is used. When Web Apps created by Google Apps Script is used, the Web Apps is used without the authorization process because when Web Apps is deployed, the authorization process is done. And, in this case, the Spreadsheet service can be used. So it is not required to use Sheets API. And, I think that Web Apps can be used as the wrapper API for Sheets API you are using. When you use this pattern, you can use Web Apps by an HTTP request.
References:
Service accounts
Create a service account
Web Apps

Google Smart Device API Oauth2 Refresh Token Expires

I am building a server side app to periodically access information from users’ Google devices and notify them when certain criteria are met.
I have a few test users signed up, and they have gone through the Google oauth2 login. The app has received their auth code, requested offline access, and stored their refresh tokens in a separate DB location. The app then polls their smart devices periodically using the refresh token to obtain access as needed.
My problem is that the test users’ refresh tokens are expiring after a few days of use. I couldn’t find any documentation from Google about refresh token longevity. Is a few days normal for a refresh token to expire?
If so, my server side app will require user intervention way too often to be useful.
https://developers.google.com/identity/protocols/oauth2#expiration
The sandbox tokens are good for a week but if your application is released officially then you can use the regular refresh tokens.

GoogleAPI oauth2 refresh token expires in 1 hour

I am using Google APIs in my application and the oauth2 refresh token expires after 1 hour. I am using this refresh token to execute a task which runs daily. I create the refresh token using the OAuth2 playground. Is there a way to extend the expiration time of a refresh token? (1 month)
I think that you have your terms confused here.
As per Oauth2 access tokens expire after one hour. Access tokens are used to request access of an api and return the data that you need. There is no way to extend the lifetime of an access token beyond one hour. You need to use a refresh token to request a new access token.
Refresh tokens are extremely long lived and do not normally expire. Refresh tokens are used to request a new access token. Refresh tokens for the most part do not expire if one is not used in six months though google will automatically expire it. Also if the user removes your access then the refresh token will also automatically expire.
If you are creating your refresh token using the Outh2 playground which is intended only for testing purposes it will also expire.
If you are using the oauth2 playground to create your refresh token then you should not be doing this you should be creating your own application to request the tokens.
As already explained the refresh tokens created using the OAuth 2.0 Playground are automatically revoked after a few hours because the playground is mainly for testing purposes. However you can configure the OAuth playground to use your own app credentials (use the 'wheely' icon top right). If you use your own app credentials the refresh token will not be revoked.
That said it looks like you want to run a background service that accesses Google APIs. For this you may want to use a Service Account if you are not accessing a specific user's data.

How to get new FacebookSDKAccessToken before it has been expired?

I have iPhone application with an functionality of Login with facebook including few other functionalities related to graph API.
I receives facebook access token and save it to the server and server then makes further requests to graph API call from web end. As facebook access token expires after 60 days, I am getting access token expired error.
To solve this issue can i use FBSDKAccessToken.refreshCurrentAccessToken method to get new Access token ?
I did not find any clear idea for FBSDKAccessToken.refreshCurrentAccessToken.
OR Is there any other way to get new access token by performing automatic login with facebook from our application.
Thanks.
Native mobile apps using Facebook's SDKs will get long-lived access
tokens, good for about 60 days. These tokens will be refreshed once
per day when the person using your app makes a request to Facebook's
servers. If no requests are made, the token will expire after about 60
days and the person will have to go through the login flow again to
get a new token.
From the FB docs. ^
It seems the user will have to go through the login process in the app once again.

Resources