How to call Tencent OCR General print API by Android

I try to call Tencent OCR generic print API ( https://cloud.tencent.com/doc.

) on Android

Authentication signature is as follows:

package com.hsare.tools;






import android.annotation.TargetApi;
import android.os.Build;
import android.util.Base64;


import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Sign {

    /**
     *  Authorization 
     *
     * @param appId
     * @param secretId
     * @param secretKey
     * @param bucketName
     * @param expired
     * @return
     * @throws Exception
     */
    public static String appSign(long appId, String secretId, String secretKey, String bucketName,
                                 long expired) throws Exception {
        long now = System.currentTimeMillis() / 1000;
        int rdm = Math.abs(new Random().nextInt());
        String plainText = String.format("a=%d&b=%s&k=%s&e=%d&t=%d&r=%d", appId, bucketName,
                secretId, now, now + expired, rdm);
        byte[] hmacDigest = HmacSha1(plainText, secretKey);
        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
                plainText.getBytes().length);
        return Base64Encode(signContent);
    }

    /**
     *  base64 
     *
     * @param binaryData
     * @return
     */

    @TargetApi(Build.VERSION_CODES.O)
    public static String Base64Encode(byte[] binaryData) {
        String encodedstr = Base64.encodeToString(binaryData, Base64.NO_WRAP);//
        return encodedstr;
    }

    /**
     *  hmacsha1 
     *
     * @param binaryData
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }

    /**
     *  hmacsha1 
     *
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(String plainText, String key) throws Exception {
        return HmacSha1(plainText.getBytes(), key);
    }
}

post request code is as follows:

package com.hsare.ocrtest;


import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hsare.tools.Config;
import com.hsare.tools.Sign;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;

import okhttp3.OkHttpClient;
import okhttp3.Request;

import okhttp3.Response;



public class MainActivity extends AppCompatActivity {

    private static final String TAG = "SSSSSSSSSSSSSS:";
    private String sign = "";
    private String url1 = "https://recognition.image.myqcloud.com/ocr/general";
    private String url2 = "https://img.codeshelper.com/upload/img/2021/04/03/2nq4chpq3yc15111.jpg";
    OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    

    //post 
    public void doPost(View view) {

        OkHttpClient client = new OkHttpClient();

        try {
            //
            sign = Sign.appSign(Config.APPID, Config.SECRETID, Config.SECRETKEY, "sd", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //
        FormBody formBody = new FormBody.Builder()
                .add("appid", "1251169844")
                .add("url",url2)
                .build();

        Log.e(TAG, "sign: "+sign );

        //
        Request request = new Request.Builder()
                .post(formBody)
                .url(url2)
                .addHeader("host","recognition.image.myqcloud.com")
                .addHeader("content-type","application/json")
                .addHeader("authorization",sign)
                .build();


            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e(TAG, "onFailure: ", e);
                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    new Thread() {
                        @Override
                        public void run() {

                            Looper.prepare();
                            Log.e(TAG, "response: "+response );
                            Looper.loop();
                        }
                    }.start();
                }
            });




    }

}


setting of Config:

package com.hsare.tools;

public class Config {

    public static final long APPID = 1251169844;
    public static final String SECRETID = "AKIDkNo6D8nSxIRPBloLOHzVs43pOlwvxS8d";
    public static final String SECRETKEY = "TnvrbCbopfJkz0pTvDLK9nIbidP1Qjpb";




}

but the result returned is this: response: Response {protocol=h2, code=405, message=, url= https://img.codeshelper.com/upload/img/2021/04/03/2nq4chpq3yc15111.jpg}

where did I make a mistake? Can I get some help?

Apr.03,2021

made it out by himself.
signature section:

mainly the stitching given by Tencent is:

String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName,
                secretId, now, now + expired, rdm);

where aversion% d

just change it to averse% s .

String.format("a=%s&k=%s&e=%d&t=%d&r=%d", appId, secretId, now + 2592000, now, rdm);

part of the code:

            //
            String json = jsonObject.put("appid", Config.APPID)
                                    .put("url", Config.image_url).toString();
            //
            sign = Sign.appSign(Config.APPID, Config.SECRETID, Config.SECRETKEY);
            Log.e(TAG, "Sign:" + sign);

            RequestBody body = RequestBody.create(JSON, json);


            //
            final Request request = new Request.Builder()
                    .header("Host", "recognition.image.myqcloud.com")
                    .header("Content-Type", "application/json")
                    .header("Authorization", sign)
                    .post(body)
                    .url(Config.code_url)
                    .build();
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e71e-2c41f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e71e-2c41f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?