How do I supply Header info such as Package Name and SHA1 in Google API request?

被刻印的时光 ゝ 提交于 2019-12-08 11:23:08

问题


I am trying to access Google API using dotNet SDK. When I don't have API restriction, I can just access and get JSON using the following code. I can still access with IP address restriction. But when I want to use with application restriction, I have to provide Package Name and SHA-1, which I already generated.

I just don't know how to provide this header info when requesting API get. There are several ways to implement the credentials. By using the service account, I only need to provide email and private key, but I don't want to use OAuth access. Please let me know if there is the best way to implement it.

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = "****",
    // HttpClientInitializer = credential,
    ApplicationName = "*****"
});

Similar solution but it's in Java


回答1:


So I found the right way to supply Package Name and SHA1. Now I can get the Json return correctly with app restriction.

youtubeService.HttpClient.DefaultRequestHeaders.Add("X-Android-Package", AppInfo.PackageName);
youtubeService.HttpClient.DefaultRequestHeaders.Add("X-Android-Cert","******");

SHA-1 certificate fingerprint must be in a lowercase string without semicolon while sending it in an API request header.

And is there a way to get the SHA1 in code instead of simply putting it as a string? Please also let me know if there is a nicer way to code this youtubeService.HttpClient.DefaultRequestHeaders.Add

I got the clue from this post.




回答2:


Xamarin and the Google APIs .net client library

You appear to be trying to use the Google apis .net client library with Xamarin. The Google apis .net client library does not support Xamarin.

Please see issue Investigate Xamarin support #984

I suspect you will at very least need to code all of the authentication yourself after that you may be able to get some of the class libraries to work. My investigation leads me to believe that the main issue with the library and Xamarin is the authorization implementation.

Service account and YouTube

The YouTube API does not support service account authentication you will need to use Oauth2 and authenticate your user that way.



来源:https://stackoverflow.com/questions/54898496/how-do-i-supply-header-info-such-as-package-name-and-sha1-in-google-api-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!