如何调用CustomerLicense:获取方法? .NET客户端库的授权步骤是什么?

时间:2015-01-08 11:25:28

标签: google-oauth google-apps-marketplace

我在Google Apps Marketplace上有一个应用。 我需要发现,任何域都安装了我们的应用程序或实际状态。 对于此Google Provide Api功能

https://developers.google.com/apps-marketplace/v2/reference/customerLicense/get

当我使用2腿认证失败时..

那么如何处理C#客户端库或Rest API ..

请帮助..

我使用我的项目编号作为应用程序ID ...

对于授权流程I使用以下方法

public HttpWebRequest GenerateRequest(string contentType, string requestMethod = "GET")
    {
        var ts = UnixTime.ToUnixTime(DateTime.Now);
        var param = new OAuthParameters()
        {
            ConsumerKey = _consumerKey,
            SignatureMethod = SigningProvider.SignatureMethod,
            Version = Constants.Version1_0,
            Nonce = NonceProvider.GenerateNonce(ts),
            Timestamp = ts.ToString()
        };

        //Generate Signature Hash
        var signatureBase = SignatureBase.Create(requestMethod.ToUpper(), _serviceProviderUri, param);
        //Set Signature Hash as one of the OAuth Parameter
        param.Signature = SigningProvider.ComputeSignature(signatureBase, _consumerSecret, null);

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(_serviceProviderUri);
        httpWebRequest.Method = requestMethod;
        httpWebRequest.ContentType = contentType;
        httpWebRequest.Timeout = RequestTimeOut;
        //Add the OAuth Parameters to Authorization Header of Request

        httpWebRequest.Headers.Add(Constants.AuthorizationHeaderParameter, param.ToHeaderFormat());
        if (requestMethod != "GET")
        {
            String s1 = new JavaScriptSerializer().Serialize(param);
            var bs = Encoding.UTF8.GetBytes(s1);
            using (Stream reqStream = httpWebRequest.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
        }
        return httpWebRequest;
    }

获取响应我使用了这个

public string GetCustomerLicense(HttpWebRequest req)
    {
        string result = "";
        try
        {
            using (WebResponse response = req.GetResponse())
            {
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                    sr.Close();
                }
            }
        }
        catch (WebException ex)
        {
            var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
            result = resp;
        }
        catch (Exception ex)
        {
            var resp = "Message = " + ex.Message;
            result = resp;
        }
        return result;
    }

以下用于调用CustomerLicence API的方法

void CallApi()
    {
        string domainName = "sampledomain.com";
        string clientId = "0000000000";//My projectNumber
        string clientSecret = "MyClientSecret";
        string getCustomerDetails = String.Format("https://www.googleapis.com/appsmarket/v2/customerLicense/{0}/{1}", clientId, domainName);
        GoogleAppsApi gapi = new GoogleAppsApi(getCustomerDetails, clientId, clientSecret);
        HttpWebRequest req = gapi.GenerateRequest("application/json", "GET");
        string result = gapi.GetCustomerLicense(req);
        resultDiv.InnerText = result;
    }

...谢谢

0 个答案:

没有答案