Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型Base64的方法encode(byte[],int)未定义。后API调用_Java_Base64 - Fatal编程技术网

Java 类型Base64的方法encode(byte[],int)未定义。后API调用

Java 类型Base64的方法encode(byte[],int)未定义。后API调用,java,base64,Java,Base64,如何解决代码中这一行的错误 encodedBytes = Base64.encode(authorization.getBytes(), 0); it is saying undefined method. 下面是完整的代码 package postapicall; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; public class PostApi1 {

如何解决代码中这一行的错误

encodedBytes = Base64.encode(authorization.getBytes(), 0);  it is saying undefined method.
下面是完整的代码

package postapicall;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class PostApi1 {
    
     public static void main(String []args)
     {
         
    
    try
    {
        String authorization = "";
        String url= "https://idcs-82972921e42641b1bf08128c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token";
        String username = "idcs-oda-9417f93560b94eb8a2e2a4c9aac9a3ff-t0_APPID";
        String password = "244ae8e2-6f71-4af2-b5cc-xxxxx";
        URL address = new URL(url);
        HttpURLConnection hc = (HttpURLConnection) address.openConnection();

        hc.setDoOutput(true);
        hc.setDoInput(true);
        hc.setUseCaches(false);

        if (username != null && password != null) {
            authorization = username + ":" + password;
        }

        if (authorization != null) {
            byte[] encodedBytes;
            encodedBytes = Base64.encode(authorization.getBytes(), 0);
            authorization = "Basic " + encodedBytes;
            hc.setRequestProperty("Authorization", authorization);
        }

   }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    
}

}

基本要求是我必须通过POST方法调用上述端点来接收承载令牌。我正在发送用户名和密码,作为回报,我希望从端点获得令牌。

错误消息准确地说明了问题所在:

The method encode(byte[], int) is undefined for the type Base64
它告诉您Base64类上没有签名为encodebyte[],int的方法

假设您使用的是Java8,让我们看一下。 我根本找不到任何名为encode的方法,所以 java的错误消息似乎是正确的

但是,有一些静态方法可以获得实际编码器,如下所示:

Base64.Encoder encoder = Base64.getEncoder();
此编码器提供您需要的实际编码方法:

String encoded = encoder.encodeToString(contentBytes);
因此,要修复该错误,替换就足够了

// original source (indentation changed)
if (authorization != null) {
    byte[] encodedBytes;
    encodedBytes = Base64.encode(authorization.getBytes(), 0);
    authorization = "Basic " + encodedBytes;
    hc.setRequestProperty("Authorization", authorization);
}


请编辑问题并添加完整的错误消息。这些是真实的凭据用户名和密码吗?您刚刚将其发布给internet上的任何人。凭据已更改。它们不是真实的。我在主题行中添加了错误。您是指com.sun.org.apache.xml.internal.security.utils.Base64吗?
// Fixed usage of Base64
if (authorization != null) {
    authorization = "Basic " + Base64.getEncoder().encodeToString(authorization.getBytes());
    hc.setRequestProperty("Authorization", authorization);
}