Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Created October 16, 2017 13:32
Show Gist options
  • Save salrashid123/3073fa70ba133213313f4354cce1643c to your computer and use it in GitHub Desktop.
Save salrashid123/3073fa70ba133213313f4354cce1643c to your computer and use it in GitHub Desktop.
Proxy-GoogleCloudCS.cs
string CREDENTIAL_FILE_PKCS12 = "/path/to/your/service0-account.p12";
string serviceAccountEmail = "[email protected]";
var certificate = new X509Certificate2(CREDENTIAL_FILE_PKCS12, "notasecret",X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
//Scopes = new[] { StorageService.Scope.DevstorageReadOnly, PublisherClient.DefaultScopes },
Scopes = PublisherClient.DefaultScopes.Append(StorageService.Scope.DevstorageReadOnly),
HttpClientFactory = new ProxySupportedHttpClientFactory()
}.FromCertificate(certificate));
StorageService service = new StorageService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = StorageClientImpl.ApplicationName,
HttpClientFactory = new ProxySupportedHttpClientFactory(),
});
var client = new StorageClientImpl(service, null);
foreach (var b in client.ListBuckets(projectID))
Console.WriteLine(b.Name);
ChannelCredentials channelCredentials = credential.ToChannelCredentials();
Channel channel = new Channel(PublisherClient.DefaultEndpoint.ToString(), channelCredentials);
PublisherSettings ps = new PublisherSettings();
PublisherClient publisher = PublisherClient.Create(channel,ps);
foreach (Topic t in publisher.ListTopics(new ProjectName(projectID)))
Console.WriteLine(t.Name);
}
}
public class ProxySupportedHttpClientFactory : HttpClientFactory
{
protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
{
//ICredentials credentials = new NetworkCredential("user1", "user1");
//var proxy = new WebProxy("http://127.0.0.1:3128", true, null, credentials);
var proxy = new WebProxy("http://127.0.0.1:3128", true, null, null);
var webRequestHandler = new HttpClientHandler()
{
UseProxy = true,
Proxy = proxy,
UseCookies = false
};
return webRequestHandler;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment