BigQuery and GCM - google-bigquery

I'm trying to create an Android app that extracts information from a table in BigQuery. In order to do this, do I have to have to implement Google Cloud Messaging? If so, do I have to have a GCM server? Is there a way, the app can authenticate directly with BigQuery and fire a query?

If you just want to make calls to BigQuery, you shouldn't need anything like Google Cloud Messaging (I'm not actually sure what that is).
You can either use the BigQuery java client (info here) or you can make raw http requests to BigQuery (you'll need to use Oauth2 for authorization).
You also might consider using an appengine app to proxy the requests to bigquery. That can make auth easier, so you don't need your BigQuery credentials in the android app.

I was trying to do the same.
I am able to do it in the Xamarin Studio(which uses C# API library of Google APIs).
Well..
First of all you need to authenticate the user..
The authentication is done using
private readonly static Google.Apis.Authentication.OAuth2.GoogleAuthenticator Auth = new Google.Apis.Authentication.OAuth2.GoogleAuthenticator (clientId,new Uri ("https://www.example.com/oauth2callback"), Google.Apis.Bigquery.v2.BigqueryService.Scopes.Bigquery.GetStringValue ());
Intent authIntent = Auth.GetUI (v.Context);
StartActivityForResult (authIntent, 1);//This will open and ask for Google account
So Auth is the Authorisation object.
var Service = new Google.Apis.Bigquery.v2.BigqueryService (Auth); //Creates the BigQuery Service
string query = "SELECT year, SUM(record_weight) as births FROM publicdata:samples.natality GROUP BY year";
Google.Apis.Bigquery.v2.JobsResource j = Service.Jobs;
Google.Apis.Bigquery.v2.Data.QueryRequest qr = new Google.Apis.Bigquery.v2.Data.QueryRequest ();
qr.Query = query;
Google.Apis.Bigquery.v2.Data.QueryResponse response = j.Query (qr, projectId).Fetch ();
Google.Apis.Bigquery.v2.Data.TableRow row = response.Rows.FirstOrDefault ();
Google.Apis.Bigquery.v2.Data.TableCell cell = row.F.FirstOrDefault ();
The above code is with respect Google APIs .NET library.
I am trying to use the Java API library.
I'll update my answer as soon as I am successful.
Hope the above answer gives you a starting point or an idea till then. :)

Related

How to fetch private data using Googlesheets API to my website using nodeJs

I need to access some data from a private Google Sheets document that only my google account has access to – it is not shared with anyone else.
From here: https://developers.google.com/sheets/guides/authorizing
When your application requests private data, the request must be
authorized by an authenticated user who has access to that data.
Again, that user would be me – the application developer. The users of my application will not have these sheets shared with them.
From what I’m reading in the Google API docs, I’m not sure this is possible – but it seems to me like it should be. I am using nodeJs.
Can anyone help me out?
You'll want to use Google's OAuth flow (here) in order to get access to private sheets. If only you need to be able to access the sheet then you can keep the OAuth as only tied to yourself and make requests to the endpoint in your app.
Example Implementation                                                                                
View in Fusebit
// Create a sheet object for the client + auth
const sheets = google.sheets({
version: 'v4',
auth
})
// Pull the data from our spreadsheet
const data = (await sheets.spreadsheets.values.get({
// See: https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms', // ID of the spreadsheet
range: 'Class Data!A2:E', // This gets all of the data in the sheet that we care about
})).data;
// Your rows/columns go into "data.values"
const rows = data.values;
});
Service account with domain-wide delegation:
The only way to avoid user interaction when accessing user private data is to use a service account with domain-wide delegation to impersonate your regular account.
That requires being a domain admin, though, so I'm not sure this option is available to you.
Sharing the spreadsheet with a service account:
Another option would be to share the spreadsheet with the service account, so you can access it using that account. Service accounts are not linked to a specific user, and so follow a different OAuth flow which doesn't require user interaction. See reference below.
Otherwise:
If you are not a domain admin and you don't want to share the spreadsheet with a service account, you are left with the web server workflow, which requires user interaction: you just cannot bypass this interaction.
I'd suggest you to read the references linked below.
Reference:
Using OAuth 2.0 for Web Server Applications
Using OAuth 2.0 for Server to Server Applications

Using Google Class room Java API in Tomcat Server as a rest API

I want to use Google Classroom Java API in Tomcat Server as a REST API.
I want the client-side code to generate an access token and refresh token and pass them to the REST API endpoints.
The REST API then use the above token to call the following code to get the list of Courses: objectOf(Classroom).courses().list().setPageSize(100).execute();
I am creating the Classroom as follows, where .getCredentials() uses
GoogleAuthorizationCodeFlow.Builder to create the credential required, but it opens a browser window to authorize the user and get the token. This doesn't work for me.
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Classroom classRoom = new Classroom.Builder(httpTransport, JSON_FACTORY, getCredentials(httpTransport))
.setApplicationName(APPLICATION_NAME).build();
I understand that you want to receive access and refresh tokens to later use them in Classroom; and you want to run this operation from a server. If that is correct, you would need to login as your account to prove your identity and receive the credentials, but there are some alternatives.
Since you are going to execute the code from a server you could follow these steps to create your credentials. Those credentials need to be saved in your work folder once and they can be read on every run.
Alternatively, you could create a service account and use it to reach your goals if you want to interact with Classroom as a different user of your organization. Please, remember to activate domain-wide delegation for this step. Don't hesitate to write back if you have some questions.

Amazon AWS for Android not accepts Google token Id, as identity provider

I acquire the Google tokenId and pass it to the CredentialsProvider instance based on the instructions here.
String token = AccountUtils.getAuthToken(this);
Map<String, String> logins = new HashMap<>();
logins.put("accounts.google.com", token);
getCredentialsProvider().withLogins(logins);
Then I refresh the CredentialsProvider instance by calling below line in an AsyncTask:
getCredentialsProvider().refresh();
I get no exceptions, I expect too see a new Google Authenticated user in the cognito control panel. But I only get a new unauthenticated user. sigh
I checked that the token I am receiving from Google is just fine and that I have set the clientId in the cognito settings.
Any help is highly appreciated
After 2 days of trying to make it work, and tempted a lot to forget AWS 4ever and run my own server, I was able to find the solution with the help of this guy.
If you follow the Google instructions to integrate sign in, you will receive 2 client ids in step 2 and 4. You need to put first one in your cognito pool and the second one in your app.
Apparently Amazon has mentioned this nowhere in their documentations

getting gmail calendars by oauth credentials in c# dot net framework 2.0

I want to read google calendars through oauth credentials for my desktop application.
I am done with following things
registered with google api console
got client id ,client secret key
Now as I was looking for some examples,which tells me that
this can be achieved with dot net framework 4.0 with google calendar api v3.
But for some reasons i need to stick to dot net framework 2.0.
So how i can achieve this?
I am done with reading calendars with username and password but now need to read through oauth.
And As I was able to read contacts by oauth I am sure there will be some way to do this for calendars.
Code for conatcts :
RequestSettings ObjectRequestSetting= new RequestSettings("appname",
"consumerkey", "consumersecretkey", "user", "domain");
ContactsRequest objContactReq = new ContactsRequest(ObjectRequestSetting);
ContactsService objService = new ContactsService("appname");
To connect to OAuth2 with out using googles client librarys is a little tricky but it can be done. I normaly use this method becouse i dont like having to release third party dll's with my applications.
The first thing you need is to build the URL that will get you the AutenticationCode.
public static Uri GetAutenticationURI(Autentication MyAutentication)
{
List postData = new List();
postData.Add("client_id=<strong>{Client ID}</strong>");
postData.Add("redirect_uri=<strong>{Redirect URI}</strong>");
postData.Add("scope=<strong>{Scope}</strong>" );
postData.Add("response_type=code");
return new Uri("https://accounts.google.com/o/oauth2/auth" + "?" + string.Join("&", postData.ToArray()));
}
The URI it returns should look something like this https://accounts.google.com/o/oauth2/auth?client_id=.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/calendar.readonly&access_type=offline&approval_prompt=force&response_type=code
You need to have a webbrowser control on your form someplace. Where ever you want to call it do something like this. It will open the screen for the user to authorise your aplication.
wbAuthenticate.Url = Autentication.GetAutenticationURI(myAutentication);
When the wbAuthenticate_DocumentCompleted returns you need to rip the Autentication code out of the body of HTML. Don't bother trying to rip it from the title this isn't always corect.
Once you have an AutenticationCode you need to exchange it for a refreshtoken and a accesstoken. The access token is the one you use for all your calls to the API. You use the refreshtoken to get a new access token back after it expires which is normaly in an hour. The only thing you need to save for next time is the RefreshToken.
I have code for all that in a blog post but its pritty big and i'm not sure about spaming that amount of code here. http://daimto.com/google-api-and-oath2/

BigQuery Simple Api Authentication

I am trying to gain access to my BigQuery enabled Google API project using the .net Google APIs.
Using a console application, I am trying to authenicate first by supplying my simple API key in the URI, then just trying to get the list of projects.
The error I am receiving when I call Fetch() on the project list is: Login Required [401]
var bigqueryService = new BigqueryService{ Key = "MY-API_KEY" };
var projectList = bigqueryService.Projects.List().Fetch();
I am purposefully not using OAuth2 as we don't need any user data.
The API key simply identifies your app to the API console for quota purposes and other housekeeping. It's not authoritative for accessing BigQuery, as we do consider the BigQuery data as "user data."
If you're just trying to get an OAuth 2 access token for playing around quickly, you can use the OAuth 2 playground:
https://code.google.com/oauthplayground/
This token will be valid for one hour and can be copied/pasted as the access_token query parameter
Here's the scope for BigQuery to use in the playground:
https://www.googleapis.com/auth/bigquery
In the end, you'll either want to use the native client (out of band) flow:
https://developers.google.com/accounts/docs/OAuth2InstalledApp
Or the server-to-server (service accounts) flow:
https://developers.google.com/accounts/docs/OAuth2ServiceAccount
I don't have quick samples handy for those in .NET, but post another question on SO if you can't find them-- I'm sure someone will chip in!
You won't be able to use a simple API key - all authorization to the BigQuery API must happen via user interaction, or alternatively through a service account.