How to pass google credential to google pub sub - google-app-engine

I am trying publish to google PubSub from my .NET Core App. I have already created the PubSub and a Topic in Google PubSub. I downloaded the private key json file and included in my project. I am able to read the file and create the credential but I don't see a way to pass the credential to Google's PubSub Publisher client. I have looked at the GitHub Post but unfortunately I am not seeing a property named DefaultEndPoint.
Running my code generates an error which is looking for environment variable for credential. I do not want to set the environment variable for now and if there is no alternative I will try that.
Following is my code and let me know what I am doing wrong of is something that got changed recently.
var credential = GoogleCredential.FromFile("app-services.json");
PublisherClient publisher = PublisherClient.CreateAsync(topicName).Result;

Agreed that this isn't at obvious as it should be.
using Grpc.Auth;
var credential = GoogleCredential.FromFile("app-services.json");
var createSettings = new PublisherClient.ClientCreationSettings(
credentials: credential.ToChannelCredentials());
var publisher = await PublisherClient.CreateAsync(topicName,
clientCreationSettings: createSettings);

Creating a PublisherServiceApiClient without environment variables:
var credential = GoogleCredential.FromJson(#"{""your_credentials_json_itself"":""here""}");
var channel = new Grpc.Core.Channel(PublisherServiceApiClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var publisher = PublisherServiceApiClient.Create(channel);

Check out the "Passing the path to the service account key in code" (for .NET) here:
https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually

Related

DotNet GMail Quickstart application name

I wrote my first Gmail API program in C# starting with the help I found
at:
https://developers.google.com/gmail/api/quickstart/dotnet
The problem is the name of the app, according to Google, listed under "Third-party apps with account access", is "Quickstart", which is not a very descriptive name for my app. Am I stuck with that name?
I've tried to search the source files for "Quickstart" and can't find out where the name is coming from. I've made sure to specify my preferred name in the ApplicationName parameter of the service connect.
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "EMailChecker",
});
But that makes no difference. I've tried removing access via my Google account and re-running the program to let it authenticate, but that does not affect. It still shows up as "Quickstart".
So am I stuck with that name? Other than that, the program is working well.
It is also stated in the documentation that if you are modifying these scopes, delete your previously saved credentials at ~/.credentials/gmail-dotnet-quickstart.json.
You may check this link for reference: Where is ~/.credentials/drive-python-quickstart.json saved for Google Drive API Python Implementation?

How to access Google Datastore API with credentials without using environment variables?

Currently I'm getting access to the API though the GOOGLE_APPLICATION_CREDENTIALS environment variable. I created and downloaded a service account key as a .json file and set the environment variable to that file. Due to the requirements of the project I'm working on, I won't be able to do this. I tried using the credentials variable in the constructor for the datastore client object, but I wasn't able to get that to work. How should I be going about this?
I'm running Windows 10, but any solution should be (relatively) OS agnostic. I'm writing in python 3.6.
You can use from_service_account_json():
from google.cloud import datastore
client = datastore.Client.from_service_account_json('/path/to/credentials.json')
var = client.get(client.key('MyKind', '<key value>'))
print(var)
see: https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually

How to troubleshoot "Value cannot be null. Parameter name: baseUri"

I need help diagnosing this error. Others get it on various platforms (youtube, for example) but seems not solved for most of them. For those who do report success, their solutions haven't work for me.
I'm using Google Drive API and I receive this error when attempting to upload to Google Drive.
My code was working fine for a long time; suddenly, I get this error. After research, I determined that I haven't reached a quota, and that these API are enabled:
Debuglet Controller API
Drive API
Google Cloud Dataflow API
Google Cloud Dataproc API
Google Cloud Deployment Manager API
Google Cloud Deployment Manager V2 API
Google Cloud Storage
Google Cloud Storage JSON API
Google Compute Engine Autoscaler API
Google Compute Engine Instance Group Manager API
Google Compute Engine Instance Group Updater API
Google Compute Engine Instance Groups API
Google Container Engine API
Google Machine Learning API
Here's my code:
string fileName = #"c:\temp\MyPicture.jpg";
string fileTitle = #"TEST-DELETE-ME.jpg";
string fileDescription = #"Test file name";
string fileType = "image/jpeg";
string parentId = "zmzmzmzmzmzmzmzmzmz";
Google.Apis.Drive.v2.DriveService service = (Google.Apis.Drive.v2.DriveService)Service;
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = fileTitle;
body.Description = fileDescription;
body.MimeType = fileType;
body.Parents = new List<ParentReference> { new ParentReference() { Id = parentId } };
byte[] byteArray = System.IO.File.ReadAllBytes(fileName);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, fileType);
Google.Apis.Upload.IUploadProgress progress = request.Upload();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
if (file == null)
{
//Here we are, and we shouldn't be!
//progress.exception.message =
//
//Value cannot be null.
//Parameter name: baseUri
}
When I look at Drive API settings, these are the configurations:
No icons (there are boxes to set 256x265, 128x128, 64x64, 32x32, and 16x16 all are blank)
The application name and short and long descriptions are all blank and optional.
For Drive Integration, all boxes and checkboxes are blank or unchecked. Specifically:
"Automatically show OAuth 2.0 consent..." is unchecked.
"Open URL" is blank.
Default and secondary MIME types and file extensions are blank
"Allow users to create new documents..." is unchecked
"Allow users to open multiple files..." is unchecked
"Allow users to open files that can be converted..." is unchecked
"This application can be launched and works properly in a mobile browser" is unchecked.
Users don't use the Google Drive UI to create documents via my application, and is not meant to be used on a mobile browser. My application only uploads PDF files to users' accounts.
My application has been working for months with no problems, now suddenly seeing this error.
I have tried disabling all API's and then re-enabling them. No dice.
I have tried using a new client id and secret. No dice.
I have tried using a new gmail address and creating a new client id and secret. No dice.
I can't possibly be the only person in the world with this problem. Can anyone help me troubleshoot this?
The underlying issue is like an authorization issue or some other error with the request. However, there's a bug in the .NET client that is masking the error.
See https://github.com/google/google-api-dotnet-client/issues/456

Does the GCE/Eclipse magic add client IDs automagically?

Or put another way...
I just moved my app from aaa.appspot.com to a new project (and hence new Client ID) called bbb.appspot.com. When I went to add the new Client Id to my GCE API definitions, it was already there!
So, on the one hand, it's magic :-)
On the other hand, is all of this documented anywhere? I have a slight concern about how I would go about overriding this behaviour if ever I want to manually configure the permitted client IDs.
I assume you are talking about the API annotations for GCE like this:
#ApiMethod(
name = "testAuth",
clientIds = { WEB_CLIENT_ID,
APP1_ANDROID_CLIENT_ID, APP1D_ANDROID_CLIENT_ID,
APP2_ANDROID_CLIENT_ID },
audiences = { Config.WEB_CLIENT_ID }
)
These client ID's come from the API console, where they are created based on your app's package and the hash of your app's certificate.
So, you may create a new GAE project for your new app id, and even create a new console project connected to that new GAE project, but your old console project still exists and still has client id's that are specific to your Android project but independent of your GAE project - so they still work.

How to upload data to local datastore?

I can update the live datastore using the remote API but is there something similar for the local datastore ? My data is in CSV format.
When I try to connect locally using below code
String username = "test";
String password = "test";
RemoteApiOptions options = new RemoteApiOptions().server("localhost", 8888).credentials(username, password);
RemoteApiInstaller installer = new RemoteApiInstaller();
installer.install(options);
I get an exception :
Exception in thread "main" java.net.UnknownHostException: http
The exception is thrown at line :
installer.install(options);
The local server is running, am I connecting correctly ? Do I need to start the local remote_api server separately ?
I finally got this to work through alot of searching. The dev url/password is XXXX/XXXX
Taken from here : https://groups.google.com/forum/?fromgroups=#!topic/google-appengine-java/1cQWn0UEoMc
I havent been able to find this specified anywhere in the google app engine documentation.

Resources