What is the correct format for the API SurveyQuestionImage.Data field?

允我心安 提交于 2019-12-04 05:23:43

问题


I am working with the GCS API, attempting to create a survey with image data.

I am using the NuGet package Google.Apis.ConsumerSurveys.v2 version 1.14.0.564 on the .Net platform. I can create surveys that do not contain image data without problem. However, when I try to create a survey with image data I receive an error from the API.

I have on hand base64 encoded png format image data. My images display properly in an IMG tag on a web page when the src attribute is set to

'data:image/png;base64,<image base64 string>'  

I want to send this image data to the API to populate the survey image. My understanding is that I need to set the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object to a string containing the image data. I have not been successful.

I first decode my base64 string to a byte array:

byte[] bytes = Convert.FromBase64String(<image base64 string>);

I have tried setting the Data property in the SurveyQuestionImage object as:

image.Data = Encoding.Unicode.GetString(bytes);

This results in this error from the API:

Google.Apis.Requests.RequestError Invalid value for ByteString: <the Data string>

I have also tried converting the byte array to a hexadecimal encoded string as:

StringBuilder sb = new StringBuilder(bytes.Length);
foreach (Byte b in bytes)
{
  sb.Append(b.ToString("X2"));
}
image.Data = sb.ToString();

This results in the more hopeful error:

Google.Apis.Requests.RequestError Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112 [400] Errors [ Message[Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112] Location[ - ] Reason[INVALID_VALUE] Domain[global] ] 

Does anyone know the correct format for the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object?


回答1:


The data needs to be base64 encoded and also "urlsafe" or "websafe" depending on what language you are using. (python and java, respectively)

In other words, you'll need to first base64 encode then:

Web safe encoding uses '-' instead of '+', '_' instead of '/'

Hope this helps!

For c# users, check out this technique for making websafe b64:

How to achieve Base64 URL safe encoding in C#?

For .net users, look at the comments in this question:

Converting string to web-safe Base64 format

And also this link for more info about .net specific options for encoding:

http://www.codeproject.com/Tips/76650/Base-base-url-base-url-and-z-base-encoding


And to specifically answer the original poster, try this for converting your byte array to a string.

    public static string ToBase64ForUrlString(byte[] input)
    {
        StringBuilder result = new StringBuilder(Convert.ToBase64String(input).TrimEnd('='));
        result.Replace('+', '-');
        result.Replace('/', '_');
        return result.ToString();
    }


来源:https://stackoverflow.com/questions/38575967/what-is-the-correct-format-for-the-api-surveyquestionimage-data-field

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